随笔分类 - TypeScript
摘要:The Block, Element, Modifier methodology (BEM) is a popular naming convention for classes in CSS. For example, the block component would be represente
阅读全文
摘要:If you're thinking about putting a TypeScript package up to NPM, you should be considering preconstruct. It makes setup EXTREMELY easy and takes many
阅读全文
摘要:You can DRY up your generics code MASSIVELY (and improve perf) by assigning local variables to default generic slots. Here, we move some complex 'Extr
阅读全文
摘要:You can use generics to dynamically specify the number, and type, of arguments to functions. Here, we create a sendEvent function which only asks for
阅读全文
摘要:Recursively flatten array up to depth times. For example: type a = FlattenDepth<[1, 2, [3, 4], [[[5]]]], 2> // [1, 2, 3, 4, [5]]. flattern 2 times typ
阅读全文
摘要:Accessing object values and array members is MUCH more powerful in the type world than it is in the runtime world. Passing a union... RETURNS a union!
阅读全文
摘要:Implement the type version of lodash's _.flip. Type FlipArguments<T> requires function type T and returns a new function type which has the same retur
阅读全文
摘要:Implement the type version of Array.reverse For example: type a = Reverse<['a', 'b']> // ['b', 'a'] type b = Reverse<['a', 'b', 'c']> // ['c', 'b', 'a
阅读全文
摘要:This one little tip has saved me hours of refactoring time. Passing string | undefined instead of ?: string ensures that ALL call sites must be given
阅读全文
摘要:The "noUncheckedIndexedAccess" is the most awesome config option you've never heard of. It makes accessing objects a lot safer, and also powers up Typ
阅读全文
摘要:Mapping over a union type can feel tricky to conceptualise. But actually, TypeScript does it all for you - using Distributive Conditional Types. Here,
阅读全文
摘要:Globals in TypeScript?! 🤯 declare global is a super useful tool for when you want to allow types to cross module boundaries. Here, we create a Global
阅读全文
摘要:Given a tuple type T that only contains string type, and a type U, build an object recursively. type a = TupleToNestedObject<['a'], string> // {a: str
阅读全文
摘要:Implement the type version of Array.shift For example type Result = Shift<[3, 2, 1]> // [2, 1] /* _____________ Your Code Here _____________ */ type S
阅读全文
摘要:Implement the type version of Object.entries For example interface Model { name: string; age: number; locations: string[] | null; } type modelEntries
阅读全文
摘要:Implement the generic Mutable<T> which makes all properties in T mutable (not readonly). For example interface Todo { readonly title: string readonly
阅读全文
摘要:Want to turn a module into a type? You can use typeof import('./') to grab the type of any module, even third-party ones. Here, we create a type from
阅读全文
摘要:Implement a generic RequiredByKeys<T, K> which takes two type argument T and K. K specify the set of properties of T that should set to be required. W
阅读全文
摘要:Implement a generic PartialByKeys<T, K> which takes two type argument T and K. K specify the set of properties of T that should set to be optional. Wh
阅读全文
摘要:Implement EndsWith<T, U> which takes two exact string types and returns whether T ends with U For example: type a = EndsWith<'abc', 'bc'> // expected
阅读全文