[TypeScript] Combine Template Literal Types usage, Extract and default Generic type
typescript and type Default Template types Usage generic
2023-09-14 08:59:12 时间
For example we have a type like this:
type Obj = { a: "a"; a2: "a2"; a3: "a3"; b: "b"; b1: "b1"; b2: "b2"; };
We want to constrct a new type which porps starts with letter 'a':
type keyStartingWithA = { [K in Extract<keyof Obj, `a${string}`>]: Obj[K]; };
So why this works?
Extract: get shared key from 'keyof Obj' (which is "a" | "a1" | "a2" | "b" | "b1" | "b2")
`a${string}` work as wired card: 'a*', * should be type string
So if we change to:
type keyStartingWithA = { [K in Extract<keyof Obj, `a${number}`>]: Obj[K]; };
OK, now we get Obj back starting with A, if we only want values of it.
type ValuesOfKeysStartingWithA = { [K in Extract<keyof Obj, `a${string}`>]: Obj[K]; }[Extract<keyof Obj, `a${string}`>];
Then we can improve the type to make it more generic
type ValuesOfKeysStartingWithA<T> = { [K in Extract<keyof T, `a${string}`>]: T[K]; }[Extract<keyof T, `a${string}`>]; type NewUnion = ValuesOfKeysStartingWithA<Obj>
Finially, you can notice that
Extract<keyof T, `a${string}`
repeated two times, we can improve those by using default type of Generic types
type ValuesOfKeysStartingWithA< T, _ExtractedKeys extends keyof T = Extract<keyof T, `a${string}`> > = { [K in _ExtractedKeys]: T[K]; }[_ExtractedKeys];
Creates a default variable _ExtractedKeys
Assign it to
Extract<keyof T, `a${string}`>
Need to use following to keep it working
extends keyof T
相关文章
- [Typescript] Tips: Create your own 'objectKeys' function using generics and the 'keyof' operator
- [Typescript] Making TypeScript Stick - 6 - infer, build a ConstructorArg Type
- [Typescript] Unknow and any Types
- [Typescript] Extends and override an existing interface
- [Typescript] Narrow the unknown Type with TypeScript's Assertion Functions
- [Typescript] Exclude Properties from a Type in TypeScript (Omit)
- [VueJS + Typescript] Decouple Dependencies Using IoC Containers in Vue with TypeScript and InversifyJS
- [TypeScript] Interface and Class
- [Typescript] Making TypeScript Stick - 6 - infer, build a ConstructorArg Type
- [React Typescript 2022] Type a Radio Button Component in React using TypeScript
- [Typescript] Prevent Type Widening of Object Literals with TypeScript's const Assertions
- [Typescript] Use the Optional Chaining Operator in TypeScript
- [Typescript] Enum: Numeric Enums and Reverse Mappings
- [Typescript] Exclude Properties from a Type in TypeScript (Omit)
- [Typescript] Specify Exact Values with TypeScript’s Literal Types
- [TypeScript] Generic Functions, class, Type Inference and Generics
- [TypeScript] Type check JavaScript files using JSDoc and Typescript 2.5
- [TypeScript] Union Types and Type Aliases in TypeScript
- [TypeScript] Using Exclude and RootDir until File Globs Lands in 2.0.
- [TypeScript] Installing TypeScript and Running the TypeScript Compiler (tsc)
- 关于COMMIT WORK and COMMIT WORK AND WAIT在SAT中的讨论