Last active
January 24, 2019 12:24
-
-
Save VadimBrodsky/4a6bb5696eb39af7a810e8c76e565356 to your computer and use it in GitHub Desktop.
This file contains 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
function getProp<T, K extends keyof T>(obj: T, key: K): T[K] { | |
return obj[key]; | |
} | |
const obj = { text: 'hello', count: 42 }; | |
let s = getProp(obj, 'text'); // string | |
let n = getProp(obj, 'count'); // number | |
let x = getProp(obj, 'foo'); // error |
This file contains 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 ObjectType = { x: T, y: U }; | |
type UnionType = T | U; | |
type IntersectionType = T & U; | |
type IndexType = keyof T; | |
type LookupType = T[K]; | |
type MappedType = { [P in K]: X } | |
type ConditionalType = T extends U ? X : Y; |
This file contains 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 Message<T> = Parameters<methods.associateVisitor>; | |
type func = (...args: any[]) => any; | |
type Unionize<T> = T[keyof T] | |
type returns = ReturnType<typeof methods.associateVisitor>; | |
// type ParameterTypes<T extends {[index: string]: (...args: any[]) => any}> = { | |
// [P in keyof T]: Parameters<T[P]> | |
// }; | |
function somefunc(...args) { | |
console.log(args); | |
} | |
type Parameters2<T extends (...args: any[]) => any> = T extends (...args: infer P) => any ? P : never; | |
type UnionToIntersection<U> = (U extends any ? (k: U)=>void : never) extends ((k: infer I)=>void) ? I : never | |
type PickAndFlatten<T, K extends keyof T> = UnionToIntersection<T[K]> | |
type GetArgumentType<original extends Function> = | |
original extends (...x: infer argumentsType) => any ? argumentsType[0] : never | |
type Unboxed<T> = T extends (infer U)[] ? U : T; | |
type Keys<T> = keyof T; | |
type UnionizeProperties<T extends object> = T[Keys<T>] | |
type ParameterTypes<T extends {[index: string]: (...args: Array<unknown>) => unknown}> = Parameters<T[keyof T]> | |
type Methods = typeof methods; | |
type Message = ParameterTypes<typeof methods>; | |
type test1 = Parameters<Methods[keyof Methods]> | |
type test2 = Parameters<typeof methods.associateVisitor>[0] | |
type test3 = UnionToIntersection<Message> | |
type test4 = Unboxed<Message> | |
type test5 = Extract<Message, []> | |
type test6 = Message['PauseMessage'] | |
type moduleToInterface<T extends {[index: string]: (...args: Array<unknown>) => unknown}> = { | |
[P in keyof T]: Parameters<T[P]>[0] | |
} | |
type test8 = moduleToInterface<Methods> | |
type test9 = UnionizeProperties<test8> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment