随笔分类 - TypeScript
摘要:Implement StartsWith<T, U> which takes two exact string types and returns whether T starts with U For example type a = StartsWith<'abc', 'ac'> // expe
阅读全文
摘要:From T, pick a set of properties whose type are assignable to U. For Example type OnlyBoolean = PickByType<{ name: string count: number isReadonly: bo
阅读全文
摘要:You cannot create a instance of abstract class. An abstract class mean to be extended. abstract class Size { constructor(public sizes: string[]) {} se
阅读全文
摘要:For example there is a clas: export class ModifierState { /** * Returns the modifier state applicable to the keyboard event given. * @param event The
阅读全文
摘要:export type PickValue<T extends object, K = keyof T> = K extends keyof T ? T[K] : never; interface Person { name: string; address: { postcode: string;
阅读全文
摘要:Ever wanted just a bit of autocomplete? Here, we create a TypeScript helper called LooseAutocomplete which gives us autocomplete while also allowing a
阅读全文
摘要:Deep partials are SUPER useful and not natively supported by TypeScript. Here, I use one to help with mocking an entity in a (imaginary) test file. ty
阅读全文
摘要:You can throw detailed error messages for type checks. Here, I move a runtime check in a function to the type level, meaning you get a detailed error
阅读全文
摘要:Just for fun... Given a number (always positive) as a type. Your type should return the number decreased by one. For example: type Zero = MinusOne<1>
阅读全文
摘要:You can use generics in React to make incredibly dynamic, flexible components. Here, I make a Table component with a generic 'items' type. interface T
阅读全文
摘要:Drop a specified char from a string. For example: type Butterfly = DropChar<' b u t t e r f l y ! ', ' '> // 'butterfly!' /* _____________ Your Code H
阅读全文
摘要:Implement PercentageParser. According to the /^(\+|\-)?(\d*)?(\%)?$/ regularity to match T and get three matches. The structure should be: [plus or mi
阅读全文
摘要:Implement RemoveIndexSignature<T> , exclude the index signature from object types. For example: type Foo = { [key: string]: any; foo(): void; } type A
阅读全文
摘要:The looseness of Object.keys can be a real pain point when using TypeScript. Luckily, it's pretty simple to create a tighter version using generics an
阅读全文
摘要:Type helpers change the game when it comes to types in your codebase. They help TypeScript infer more from your code - and make your types a lot more
阅读全文
摘要:export const getDeepValue = <Obj, FirstKey extends keyof Obj, SecondKey extends keyof Obj[FirstKey]>( obj: Obj, firstKey: FirstKey, secondKey: SecondK
阅读全文
摘要:Implement a type IsUnion, which takes an input type T and returns whether T resolves to a union type. For example: type case1 = IsUnion<string> // fal
阅读全文
摘要:Implement a type IsNever, which takes input type T. If the type of resolves to never, return true, otherwise false. For example: type A = IsNever<neve
阅读全文
摘要:TypeScript's string interpolation powers are incredible, especially since 4.1. Add some utilities from ts-toolbelt, and you've got a stew going. Here,
阅读全文
摘要:We want to convert export type Entity = | {type: "user"} | {type: "post"} | {type: "comment"} to type EntityWithId = | {type: "user", userId: string}
阅读全文