conditional-types

Type is not assignable to conditional type

◇◆丶佛笑我妖孽 提交于 2021-02-10 03:26:23
问题 I have a TypeScript code snippet in the playground. Please take a look there at TypeScript playground or here: enum MyTypes { FIRST = "FIRST", SECOND = "SECOND", THIRD = "THIRD" } type TFirst = { type: MyTypes.FIRST foo: string } type TSecond = { type: MyTypes.SECOND foo: string } type TThird = { type: MyTypes.THIRD bar: string } type TConditionalType<T> = T extends MyTypes.FIRST ? TFirst : T extends MyTypes.SECOND ? TSecond : T extends MyTypes.THIRD ? TThird : null const

Type is not assignable to conditional type

有些话、适合烂在心里 提交于 2021-02-10 03:26:06
问题 I have a TypeScript code snippet in the playground. Please take a look there at TypeScript playground or here: enum MyTypes { FIRST = "FIRST", SECOND = "SECOND", THIRD = "THIRD" } type TFirst = { type: MyTypes.FIRST foo: string } type TSecond = { type: MyTypes.SECOND foo: string } type TThird = { type: MyTypes.THIRD bar: string } type TConditionalType<T> = T extends MyTypes.FIRST ? TFirst : T extends MyTypes.SECOND ? TSecond : T extends MyTypes.THIRD ? TThird : null const

Type is not assignable to conditional type

左心房为你撑大大i 提交于 2021-02-10 03:25:09
问题 I have a TypeScript code snippet in the playground. Please take a look there at TypeScript playground or here: enum MyTypes { FIRST = "FIRST", SECOND = "SECOND", THIRD = "THIRD" } type TFirst = { type: MyTypes.FIRST foo: string } type TSecond = { type: MyTypes.SECOND foo: string } type TThird = { type: MyTypes.THIRD bar: string } type TConditionalType<T> = T extends MyTypes.FIRST ? TFirst : T extends MyTypes.SECOND ? TSecond : T extends MyTypes.THIRD ? TThird : null const

Type is not assignable to conditional type

安稳与你 提交于 2021-02-10 03:25:07
问题 I have a TypeScript code snippet in the playground. Please take a look there at TypeScript playground or here: enum MyTypes { FIRST = "FIRST", SECOND = "SECOND", THIRD = "THIRD" } type TFirst = { type: MyTypes.FIRST foo: string } type TSecond = { type: MyTypes.SECOND foo: string } type TThird = { type: MyTypes.THIRD bar: string } type TConditionalType<T> = T extends MyTypes.FIRST ? TFirst : T extends MyTypes.SECOND ? TSecond : T extends MyTypes.THIRD ? TThird : null const

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

Typescript: using conditional typing in conditional statements

寵の児 提交于 2021-01-29 07:00:57
问题 Assume I have some a-lot-of-union type: var MyComplexType = MyType1 | MyType2 | MyType3 | ... | MyTypeN where MyType{N} has this kind of signature: type MyType1 = { type: string, data: <different data for different types> } I know that I can use a kind of type guard function, e. g.: function isMyComplexTypeOfMyType1(item: MyComplexType): item is MyType1 { return item.type == "type of MyType1" } but in this case I should write a lot of this kind of functions. So, the question is: can I