Last active
February 15, 2019 08:08
-
-
Save MrKou47/6a469b1789b7b5c4979c965171139b82 to your computer and use it in GitHub Desktop.
typescript type operate
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
const x = async (y) => { | |
return 1 | |
} | |
type Unpacked<T> = | |
T extends Promise<infer U> ? U : | |
T; | |
export type T = Unpacked<ReturnType<typeof x>> // number |
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
// Get the last item type from a tuple of types | |
type Stuff = [number, boolean, string] | |
type OtherStuff = [string, number, object, boolean] | |
type LastStuff = Stuff[2] // => string | |
type LastOtherStuff = OtherStuff[3] // => boolean | |
type GetLength<original extends any[]> = original extends { length: infer L } ? L : never | |
type LengthStuff = GetLength<Stuff> // => 3 | |
// Borrowed from SimplyTyped: | |
type Prev<T extends number> = [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62][T]; | |
// Actual, legit sorcery | |
// Borrowed from pelotom/hkts: | |
type GetLength<original extends any[]> = original extends { length: infer L } ? L : nevertype GetLast<original extends any[]> = original[Prev<GetLength<original>>] | |
// Here are our test-subject tuples: | |
type Stuff = [number, boolean, string] | |
type OtherStuff = [string, number, object, boolean] | |
// How long is the `Stuff` tuple? | |
type LengthStuff = GetLength<Stuff> // => 3 | |
// What is the last element of each tuple? | |
type LastStuff = GetLast<Stuff> // => string | |
type LastOtherStuff = GetLast<OtherStuff> // => boolean |
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 a = { name: string; sex: boolean }; | |
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>> | |
type KnownKeys<T> = { | |
[K in keyof T]: string extends K ? never : number extends K ? never : K | |
} extends {[_ in keyof T]: infer U} ? U : never; | |
type b = KnownKeys<a>; | |
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 GetReturnType<original extends Function> = | |
original extends (...x: any[]) => infer returnType ? returnType : never | |
const someRandomStuff = <fn extends Function>(originalFn: fn) => { | |
const result: GetReturnType<fn> = originalFn(12345); | |
return result; | |
} | |
const innerFn = (item: number) => item.toString(); | |
const output = someRandomStuff(innerFn); // works greate | |
// 高级用法:获取reducer的类型 | |
const reducer = { | |
homepage, // (state, action) => IHomepageState; | |
}; | |
type GetReturnType<original extends Function> = | |
original extends (...x: any[]) => infer returnType ? returnType : never | |
export type IFnReturnType<T> = { | |
[P in keyof T]: T[P] extends Function ? GetReturnType<T[P]> : T[P]; | |
}; | |
let b: IFnReturnType<reducer>; // { homepage: IHomepageState } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
函数式编程,使用替代类型来获得接下来函数的返回值类型