// ts中可以使用 typeof 获取 变量的类型
const str:string = "foo"
function echo(arg:typeof str):string {
return arg
}
// 如何获取echo函数的返回值类型?
const bar :typeof echo = '1' // ERROR
// 不能将类型“"1"”分配给类型“(arg: string) => string”。
// ts中可以使用 typeof 获取 变量的类型
const str:string = "foo"
function echo(arg:typeof str):string {
return arg
}
// 如何获取echo函数的返回值类型?
const bar :typeof echo = '1' // ERROR
// 不能将类型“"1"”分配给类型“(arg: string) => string”。
type ReturnType<T> = T extends (...args: any[]) => infer R ? R : never;
type ValueType = ReturnType<() => 123> // 123
13 回答13.1k 阅读
8 回答3k 阅读
3 回答1.5k 阅读✓ 已解决
2 回答5.3k 阅读✓ 已解决
5 回答1.6k 阅读
7 回答2.3k 阅读
9 回答1.8k 阅读✓ 已解决
翻下文档就能解决的事情