union-types

Typescript: convert a tagged union into an union type

随声附和 提交于 2021-02-04 20:59:59
问题 I've the following tagged union interface interface Example { a: TypeA; b: TypeB; } as an output I would like to convert this tagged union into an union type such as: type oneOf<T> = ... Where var example: oneOf(Example); // would be { a: TypeA } | { b : TypeB } I have not been able to map the type keys into an object having only the key as parameter. Any idea ? 回答1: You can do it with a combination of: Distributive conditional types Mapped types interface Example { a: string; b: number; }

Typescript: convert a tagged union into an union type

和自甴很熟 提交于 2021-02-04 20:58:12
问题 I've the following tagged union interface interface Example { a: TypeA; b: TypeB; } as an output I would like to convert this tagged union into an union type such as: type oneOf<T> = ... Where var example: oneOf(Example); // would be { a: TypeA } | { b : TypeB } I have not been able to map the type keys into an object having only the key as parameter. Any idea ? 回答1: You can do it with a combination of: Distributive conditional types Mapped types interface Example { a: string; b: number; }

Nested Typescript Map Type

徘徊边缘 提交于 2021-01-28 22:13:57
问题 With Typescript 3.5.1, I can't seem to get the type of values to compile. For the following: type foo = 'a' | 'b' const x: Map<foo, {[key: string]: string}> = new Map([ ['a', {'ok': 'so'}], ['b', {'nah': 'right'}] ]) tsc barfs up const x: Map<foo, { [key: string]: string; }> Type 'Map<"a" | "b", { 'ok': string; 'nah'?: undefined; } | { 'nah': string; 'ok'?: undefined; }>' is not assignable to type 'Map<foo, { [key: string]: string; }>'. Type '{ 'ok': string; 'nah'?: undefined; } | { 'nah':

typescript type is lost on an object literal assign using a union type

你离开我真会死。 提交于 2021-01-27 23:22:07
问题 I would expect an error from the following code, but for typescript is perfectly ok, can you tell me why? export interface Type1 { command: number; } export interface Type2 { children: string; } export type UnionType = Type1 | Type2; export const unionType: UnionType = { command: 34234, children: [{ foo: 'qwerty' }, { asdf: 'zxcv' }], }; Here is the link. 回答1: Typescript currently does not have a concept of exact types (there is exception for object literals, I'll explain in the end). In

typescript type is lost on an object literal assign using a union type

做~自己de王妃 提交于 2021-01-27 21:56:00
问题 I would expect an error from the following code, but for typescript is perfectly ok, can you tell me why? export interface Type1 { command: number; } export interface Type2 { children: string; } export type UnionType = Type1 | Type2; export const unionType: UnionType = { command: 34234, children: [{ foo: 'qwerty' }, { asdf: 'zxcv' }], }; Here is the link. 回答1: Typescript currently does not have a concept of exact types (there is exception for object literals, I'll explain in the end). In

TypeScript: Promise.all doesn't handle union types

泪湿孤枕 提交于 2021-01-02 06:32:49
问题 I have the following piece of code that results in compile error in TypeScript: type Promisable = (() => Promise<string>) | (() => Promise<number>); const func = async (promisable: Promisable) => { await Promise.all([promisable()]); }; The error is as follows No overload matches this call. The last overload gave the following error. Argument of type '(Promise | Promise)[]' is not assignable to parameter of type 'Iterable>'. The types returned by 'Symbol.iterator.next(...)' are incompatible

Conditional data types on union data types

大兔子大兔子 提交于 2020-07-22 05:20:12
问题 Assume that you have the following types: type Foo = { prop1: 'foo', prop2: null } type Bar = { prop1: 'bar', prop2: number } type FooOrBar = Foo | Bar Is it possible to rewrite the above code using conditional types? I have tried the following: type FooOrBar = { prop1: 'foo' | 'bar' prop2: FooOrBar['prop1'] extends 'foo' ? null : number } But when trying to declare a var like so: const sample1: FooOrBar = { prop1: 'foo', prop2: null } I receive the following error: Type 'null' is not

How to extract a type from a tagged union type in typescript?

醉酒当歌 提交于 2020-06-27 07:36:19
问题 Say there is already a type defined as this: export type Item = { type: 'text', content: string } | { type: 'link', url: string } Is it possible to extract the link part from the type Item ? I mean, is it possible to define a type ExtractTypeFrom : type LinkItem = ExtractType<Item, 'type', 'link'> And the LinkItem will be: { type: 'link', url: string } 回答1: Yes it is possible you are very close, you can use the predefined Extract conditional type. You can will need to pass in, as the second

TS2339: Property does not exist on union type - property string | undefined

女生的网名这么多〃 提交于 2020-06-24 12:31:05
问题 I have problem with my union type, which looks like this: type RepeatForm = { step: | { repeat: false; } | { repeat: true; from: undefined; } | { repeat: true; from: string; by?: string; }; }; And I have following function where I want to get value of by if it's there: export const getByField = (form: RepeatForm) => { if (form.step.repeat === false || form.step.from === undefined) { return null; } const x = form.step.from; return form.step.by; }; I get this error: Property 'by' does not exist

What's the difference between a constrained TypeVar and a Union?

荒凉一梦 提交于 2020-03-16 05:51:04
问题 If I want to have a type that can represent multiple possible types, Union s seem to be how I represent that: U = Union(int, str) U can be an int or a str . I noticed though that TypeVar s allow for optional var-arg arguments that also seem to do the same thing: T = TypeVar("T", int, str) Both T and U seem to only be allowed to take on the types str and int . What are the differences between these two ways, and when should each be preferred? 回答1: T 's type must be consistent across multiple