随笔分类 - TypeScript
摘要:Requirement is if pass in initialData, then return type should not contain undefined, otherwise, it should. import { it } from "vitest"; import { Equa
阅读全文
摘要:Here's a function called returnWhatIPassInExceptFor1: function returnWhatIPassInExceptFor1(t: unknown): unknown { if (t 1) { return 2; } return t; } W
阅读全文
摘要:Union type: function runGenerator(generator: { run: () => string } | (() => string)) { if (typeof generator 'function') { return generator(); } return
阅读全文
摘要:TypeScript 5.0 now allows JSDoc to declare overloads with a new @overload tag. Each JSDoc comment with an @overload tag is treated as a distinct overl
阅读全文
摘要:https://devblogs.microsoft.com/typescript/announcing-typescript-5-0-beta/#exhaustive-switch-case-completions In the example code: type HasNames = { na
阅读全文
摘要:The following code import { expect, it } from 'vitest'; import { Equal, Expect } from '../helpers/type-utils'; export interface Cache<T> { get: (key:
阅读全文
摘要:import { expect, it } from "vitest"; import { Equal, Expect } from "../helpers/type-utils"; function youSayGoodbyeISayHello(greeting: unknown) { retur
阅读全文
摘要:Previously, we have problem for such code: type RGB = readonly [red: number, green: number, blue: number]; type Color = { value: RGB | string }; const
阅读全文
摘要:Consider this createClassNamesFactory function that's similar to one I built for getting Tailwind class names that could then be passed into a React c
阅读全文
摘要:This exercise begins with a makeStatus function that takes in an array of TStatuses that extends an array of strings. The statuses are then returned,
阅读全文
摘要:There are two solutions to this challenge, both with different ways of representing the generic. Solution 1: The first option is using TConfig which e
阅读全文
摘要:See this code: const array = [ { name: 'John', }, { name: 'Steve', }, ]; const obj = array.reduce((accum, item) => { accum[item.name] = item; // Eleme
阅读全文
摘要:Here we have a Component class that can be passed in TProps. Inside of the constructor it assigns props to this, and provides a getProps method that c
阅读全文
摘要:Consider this implementation of returnBothOfWhatIPassIn: const returnBothOfWhatIPassIn = (params: { a: unknown; b: unknown }) => { return { first: par
阅读全文
摘要:Let's we have a form to submit new password. Before we send request to server, we want to force developer to valid the password before sending the req
阅读全文
摘要:// print type PrintStart = { type: "print"; subtype: "start"; attributes: { controlsId: string; tabId: number; }; } type PrintAbort = { type: "print";
阅读全文
摘要:Let's imagine you're building a type helper to extract out the value from several different 'parsers'. Here are a few different examples of what a par
阅读全文
摘要:For example, we have a type repersent currency: USD, value should be something like '$123', a string type with `$` and number. So let's say we make a
阅读全文
摘要:Currying is the technique of converting a function that takes multiple arguments into a sequence of functions that each take a single argument. But in
阅读全文
摘要:This challenge continues from 476 - Sum, it is recommended that you finish that one first, and modify your code based on it to start this challenge. I
阅读全文