Last active
July 26, 2019 09:03
-
-
Save mrk21/b73aa799468c488fb40c2f1ae9e2a765 to your computer and use it in GitHub Desktop.
TypeScript typings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type ActionTree<T> = { | |
[ K in keyof T ]: { | |
type: K; | |
payload: T[K] extends { payload: any } ? T[K]['payload'] : never; | |
}; | |
}; | |
type HogeActionTree = ActionTree<{ | |
'hoge/get': { | |
payload: { id: string; }; | |
}; | |
'hoge/getList': { | |
payload: null; | |
}; | |
}>; | |
type FooActionTree = ActionTree<{ | |
'foo/get': { | |
payload: { id: string; }; | |
}; | |
}>; | |
type MakeActions<T> = T extends { type: string; payload: any } ? T : T[keyof T]; | |
type Actions<T> = T extends [...any[]] | |
? { [I in keyof T]: MakeActions<T[I]>; }[number] | |
: MakeActions<T>; | |
type T1 = Actions<HogeActionTree> | |
type T2 = Actions<[HogeActionTree, FooActionTree]> | |
type T3 = Actions<[HogeActionTree['hoge/get'], FooActionTree]> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type TypeMaps = { | |
string: string; | |
number: number; | |
boolean: boolean; | |
}; | |
type ConstructorOrTypename = { new(...args: any[]): any } | keyof TypeMaps; | |
type TypeFrom<T extends ConstructorOrTypename> = | |
T extends { new(...args: any[]): infer I } | |
? I | |
: T extends keyof TypeMaps | |
? TypeMaps[T] | |
: never; | |
function isType<T extends ConstructorOrTypename>(type: T) : type is TypeFrom<T> { | |
return true; | |
} | |
isType(Error); | |
isType('string'); | |
isType('number'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment