随笔分类 - TypeScript
摘要:// Before declare function createFSM<TState extends string>(config: { // Without NoInfer, TS doesn't know which // TState is the source of truth initi
阅读全文
摘要:// Before declare function useState<T>(status: T[]): T; const loadingStatus = useState(["loading", "idle"]) // string type // after declare function u
阅读全文
摘要:const routes = { user: ["get-user", "get-all-users"], comment: ["get-comment", "get-all-comments"] } as const type Routes = typeof routes type Possibl
阅读全文
摘要:Overrides A common mistake, that has historically been difficult for TypeScript to assist with is typos when overriding a class method class Car { hon
阅读全文
摘要:JS private field #serialNumbercannot be accessed outside class. But within class, it is accessible. JS private field can also be used to check class i
阅读全文
摘要:In recent Typescript, it is possible to define static block class Car { static nextSerialNumber = 100 static isReady = false static { // this is the s
阅读全文
摘要:Sometimes we have a free-standing function that has a strong opinion around what this will end up being, at the time it is invoked. For example, if we
阅读全文
摘要:interface UnionBuilder<T = never> { add: <NewValue>() => UnionBuilder<T | NewValue>, fold: () => T } declare const u: UnionBuilder; const result = u .
阅读全文
摘要:type NestedNumber = number | NestedNumber[]
阅读全文
摘要:const returnWhatIPassIn = <const T extends any[]>(t: T) => { return t; }; // result is any[] in TS 5.2, but ['a', 'b', 'c'] in 5.3 const result = retu
阅读全文
摘要:Let's say you're creating a component that has all the props of input but needs to add a label prop. You'll need to extend from the ComponentProps typ
阅读全文
摘要:PubSub is one of the most foundational patterns for reactivity. Firing an event out with publish() allows anyone to listen to that event subscribe() a
阅读全文
摘要:type WidenLiteral<T> = T extends string | number | boolean ? ReturnType<T["valueOf"]> : T; type Example1 = WidenLiteral<"abc"> // string type Example2
阅读全文
摘要:Omit on Union type type Union = | { a: "a"; user?: string; } | { b: "b"; user?: string; }; type X = Omit<Union, "user">; // X is {} Using Distributive
阅读全文
摘要:The `as` Prop in React Option 1: import { Equal, Expect } from '../helpers/type-utils'; export const Wrapper = <TProps extends keyof JSX.IntrinsicElem
阅读全文
摘要:import { Router, useRouter } from "fake-external-lib"; export const withRouter = <TProps extends { router: Router }>( Component: React.ComponentType<T
阅读全文
摘要:Fix forwardRef globally To jump ahead to the solution, uncommenting the following code from Stefan Baumgartner will globally override the value of for
阅读全文
摘要:import { Equal, Expect } from "../helpers/type-utils"; type InputProps = React.ComponentProps<"input">; const COMPONENTS = { text: (props) => { return
阅读全文
摘要:1. React.ReactNode import { useState } from "react"; import { createPortal } from "react-dom"; import { Equal, Expect } from "../helpers/type-utils";
阅读全文
摘要:Navigating to the type definition for lazy by CMD + click in local VS Code, or in the DefinitelyTyped repo. We can see the following definition: funct
阅读全文