随笔分类 -  TypeScript

上一页 1 ··· 13 14 15 16 17 18 19 20 21 ··· 30 下一页
摘要: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 阅读全文
posted @ 2022-10-14 01:38 Zhentiw 阅读(27) 评论(0) 推荐(0)
摘要:From T, pick a set of properties whose type are assignable to U. For Example type OnlyBoolean = PickByType<{ name: string count: number isReadonly: bo 阅读全文
posted @ 2022-10-14 01:31 Zhentiw 阅读(19) 评论(0) 推荐(0)
摘要:You cannot create a instance of abstract class. An abstract class mean to be extended. abstract class Size { constructor(public sizes: string[]) {} se 阅读全文
posted @ 2022-10-13 22:20 Zhentiw 阅读(27) 评论(0) 推荐(0)
摘要:For example there is a clas: export class ModifierState { /** * Returns the modifier state applicable to the keyboard event given. * @param event The 阅读全文
posted @ 2022-10-13 18:13 Zhentiw 阅读(36) 评论(0) 推荐(0)
摘要:export type PickValue<T extends object, K = keyof T> = K extends keyof T ? T[K] : never; interface Person { name: string; address: { postcode: string; 阅读全文
posted @ 2022-10-13 00:00 Zhentiw 阅读(22) 评论(0) 推荐(0)
摘要:Ever wanted just a bit of autocomplete? Here, we create a TypeScript helper called LooseAutocomplete which gives us autocomplete while also allowing a 阅读全文
posted @ 2022-10-10 14:17 Zhentiw 阅读(23) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2022-10-10 14:10 Zhentiw 阅读(153) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2022-10-09 20:41 Zhentiw 阅读(26) 评论(0) 推荐(0)
摘要: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> 阅读全文
posted @ 2022-10-07 22:57 Zhentiw 阅读(63) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2022-10-07 18:13 Zhentiw 阅读(36) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2022-10-07 01:08 Zhentiw 阅读(17) 评论(0) 推荐(0)
摘要:Implement PercentageParser. According to the /^(\+|\-)?(\d*)?(\%)?$/ regularity to match T and get three matches. The structure should be: [plus or mi 阅读全文
posted @ 2022-10-06 02:11 Zhentiw 阅读(25) 评论(0) 推荐(0)
摘要:Implement RemoveIndexSignature<T> , exclude the index signature from object types. For example: type Foo = { [key: string]: any; foo(): void; } type A 阅读全文
posted @ 2022-10-05 01:17 Zhentiw 阅读(69) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2022-10-04 14:16 Zhentiw 阅读(26) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2022-10-04 14:06 Zhentiw 阅读(14) 评论(0) 推荐(0)
摘要:export const getDeepValue = <Obj, FirstKey extends keyof Obj, SecondKey extends keyof Obj[FirstKey]>( obj: Obj, firstKey: FirstKey, secondKey: SecondK 阅读全文
posted @ 2022-10-03 18:10 Zhentiw 阅读(12) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2022-09-30 02:49 Zhentiw 阅读(16) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2022-09-28 21:47 Zhentiw 阅读(27) 评论(0) 推荐(0)
摘要: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, 阅读全文
posted @ 2022-09-28 21:37 Zhentiw 阅读(65) 评论(0) 推荐(0)
摘要:We want to convert export type Entity = | {type: "user"} | {type: "post"} | {type: "comment"} to type EntityWithId = | {type: "user", userId: string} 阅读全文
posted @ 2022-09-28 14:03 Zhentiw 阅读(8) 评论(0) 推荐(0)

上一页 1 ··· 13 14 15 16 17 18 19 20 21 ··· 30 下一页