TypeScript ReturnType

ReturnType<T>,该工具类型能够获取函数类型T的返回值类型。示例如下:

// string
type T0 = ReturnType<() => string>;

// { a: string; b: number }
type T1 = ReturnType<() => { a: string; b: number }>;

// void
type T2 = ReturnType<(s: string) => void>;

// {}
type T3 = ReturnType<<T>() => T>;

// number[]
type T4 = ReturnType<<T extends U, U extends number[]>() => T>;

// any
type T5 = ReturnType<never>;

type T6 = ReturnType<boolean>;   // 编译错误
type T7 = ReturnType<Function>;  // 编译错误

运行结果:

TypeScript ReturnType

酷客网相关文章:

赞(0)

评论 抢沙发

评论前必须登录!