mapped-types

Exclude object keys by their value type in TypeScript

会有一股神秘感。 提交于 2021-02-08 14:16:01
问题 I want to map an object type to a subtype that includes only keys whose values are of a specific type. For example, something like ExtractNumeric<T> , where ExtractNumeric<{ str: string, num: number }> should be equivalent to the type: { num: number } I've tried this, but it does not work: type ExtractNumeric<T> = { [k in keyof T]: T[k] extends number ? T[k] : never } This snippet throws a type error: let obj: ExtractNumeric<{ str: string, num: number }> = { num: 1 } Because although the str

Exclude object keys by their value type in TypeScript

為{幸葍}努か 提交于 2021-02-08 14:14:45
问题 I want to map an object type to a subtype that includes only keys whose values are of a specific type. For example, something like ExtractNumeric<T> , where ExtractNumeric<{ str: string, num: number }> should be equivalent to the type: { num: number } I've tried this, but it does not work: type ExtractNumeric<T> = { [k in keyof T]: T[k] extends number ? T[k] : never } This snippet throws a type error: let obj: ExtractNumeric<{ str: string, num: number }> = { num: 1 } Because although the str

Exclude object keys by their value type in TypeScript

为君一笑 提交于 2021-02-08 14:14:18
问题 I want to map an object type to a subtype that includes only keys whose values are of a specific type. For example, something like ExtractNumeric<T> , where ExtractNumeric<{ str: string, num: number }> should be equivalent to the type: { num: number } I've tried this, but it does not work: type ExtractNumeric<T> = { [k in keyof T]: T[k] extends number ? T[k] : never } This snippet throws a type error: let obj: ExtractNumeric<{ str: string, num: number }> = { num: 1 } Because although the str

Typescript deep replace multiple types

被刻印的时光 ゝ 提交于 2021-02-07 09:55:41
问题 I use mongodb with @types/mongodb. This gives me a nice FilterQuery interface for my mogodb queries for a collection of shaped documents. In my domain object class I have some extra logic like converting dates to moment objects or floats to BigNumber objects. For my queries I need to convert these back, so for example a Moment object needs to be converted to a date object and so on. To avoid duplication and maintaining a separate interface (just for the queries) I thought of using mapped

Inferring nested value types with consideration for intermediate optional keys

痞子三分冷 提交于 2021-02-02 03:37:42
问题 I'm trying to define helper types for determining the type of nested object values, whilst also considering any optional parent keys, e.g. in structures like these (or deeper): type Foo = { a: { b?: number; } }; type Foo2 = { a?: { b: number } }; For my purposes, the type of b in both Foo and Foo2 should be inferred as number | undefined . In Foo2 the b is not optional itself, but because a is, for my lookup purposes b must now be optional too... so much for context. Using these helper types

Create object of mapped type with a constraint

喜夏-厌秋 提交于 2021-01-29 05:43:57
问题 Similar to this question I'm trying to create an object of a mapped type. However, I keep getting type errors in the implementation. Here's a toy example: type SingleArgFunction<A> = (x: A) => A; type ArrayedReturnFunction<A> = (x: A) => A[]; // create an arrayed function from a single arg function type MakeArrayed<F> = F extends SingleArgFunction<infer A> ? ArrayedReturnFunction<A> : never; // mapped type type MakeArrayedAll<FS> = { [K in keyof FS]: MakeArrayed<FS[K]> } // example usage type

Typescript: create union instead intersection when merging optional with required type

被刻印的时光 ゝ 提交于 2020-12-31 05:49:17
问题 When optional and required property are merged via intersection, required wins type A = { who: string } type B = { who?: string } // $ExpectType {who:string} type R = A & B This may lead to runtime errors, when for instance, dealing with default params pattern within a function type Params = { who: string greeting: string } const defaults: Params = { greeting: 'Hello', who: 'Johny 5', } function greeting(params: Partial<Params>){ // $ExpectType Params const merged = {...defaults, ...params}

Typescript: create union instead intersection when merging optional with required type

不打扰是莪最后的温柔 提交于 2020-12-31 05:48:29
问题 When optional and required property are merged via intersection, required wins type A = { who: string } type B = { who?: string } // $ExpectType {who:string} type R = A & B This may lead to runtime errors, when for instance, dealing with default params pattern within a function type Params = { who: string greeting: string } const defaults: Params = { greeting: 'Hello', who: 'Johny 5', } function greeting(params: Partial<Params>){ // $ExpectType Params const merged = {...defaults, ...params}

Typescript: create union instead intersection when merging optional with required type

心不动则不痛 提交于 2020-12-31 05:48:27
问题 When optional and required property are merged via intersection, required wins type A = { who: string } type B = { who?: string } // $ExpectType {who:string} type R = A & B This may lead to runtime errors, when for instance, dealing with default params pattern within a function type Params = { who: string greeting: string } const defaults: Params = { greeting: 'Hello', who: 'Johny 5', } function greeting(params: Partial<Params>){ // $ExpectType Params const merged = {...defaults, ...params}

Typescript: create union instead intersection when merging optional with required type

╄→гoц情女王★ 提交于 2020-12-31 05:47:43
问题 When optional and required property are merged via intersection, required wins type A = { who: string } type B = { who?: string } // $ExpectType {who:string} type R = A & B This may lead to runtime errors, when for instance, dealing with default params pattern within a function type Params = { who: string greeting: string } const defaults: Params = { greeting: 'Hello', who: 'Johny 5', } function greeting(params: Partial<Params>){ // $ExpectType Params const merged = {...defaults, ...params}