conditional-types

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

Mapping enum values to types

混江龙づ霸主 提交于 2020-06-28 05:36:15
问题 The problem Suppose I have some code like this: // Events we might receive: enum EventType { PlaySong, SeekTo, StopSong }; // Callbacks we would handle them with: type PlaySongCallback = (name: string) => void; type SeekToCallback = (seconds: number) => void; type StopSongCallback = () => void; In the API I'm given, I can register such a callback with declare function registerCallback(t: EventType, f: (...args: any[]) => void); But I want to get rid of that any[] and make sure I can't

How to conditionally detect the `any` type in TypeScript? [duplicate]

邮差的信 提交于 2020-05-11 07:14:20
问题 This question already has an answer here : Typescript check for the 'any' type (1 answer) Closed 4 days ago . I would like an utility that I can use as IsStrictlyAny<T> and it will resolve to the type true if T is exactly any and to the false type otherwise. How can I do this? My first idea: type IsStrictlyAny<T> = any extends T ? true : false; Results: IsStrictlyAny<any> : true (good!) IsStrictlyAny<unknown> : true (bad! - I want false ) IsStrictlyAny<string> : boolean (bad! - I want false )

How to conditionally detect the `any` type in TypeScript? [duplicate]

巧了我就是萌 提交于 2020-05-11 07:14:08
问题 This question already has an answer here : Typescript check for the 'any' type (1 answer) Closed 4 days ago . I would like an utility that I can use as IsStrictlyAny<T> and it will resolve to the type true if T is exactly any and to the false type otherwise. How can I do this? My first idea: type IsStrictlyAny<T> = any extends T ? true : false; Results: IsStrictlyAny<any> : true (good!) IsStrictlyAny<unknown> : true (bad! - I want false ) IsStrictlyAny<string> : boolean (bad! - I want false )

Typescript: Return type of function based on input value (enum)

你。 提交于 2020-01-24 07:56:46
问题 I'm storing some settings into local storage and I would like to type the responses when I get (and ideally also insert) values from/to the storage. From what I've seen, the best way seems to be to use function overloading. So this is what I have now and it works: export enum SettingsKey { hasOnboarded = 'hasOnboarded', phoneNumber = 'phoneNumber' } export async function getSetting(storage: Storage, key: SettingsKey.phoneNumber): Promise<string> export async function getSetting(storage:

Typescript: Return type of function based on input value (enum)

霸气de小男生 提交于 2020-01-24 07:55:37
问题 I'm storing some settings into local storage and I would like to type the responses when I get (and ideally also insert) values from/to the storage. From what I've seen, the best way seems to be to use function overloading. So this is what I have now and it works: export enum SettingsKey { hasOnboarded = 'hasOnboarded', phoneNumber = 'phoneNumber' } export async function getSetting(storage: Storage, key: SettingsKey.phoneNumber): Promise<string> export async function getSetting(storage: