// 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
10 回答11.1k 阅读
6 回答3k 阅读
5 回答4.8k 阅读✓ 已解决
4 回答3k 阅读✓ 已解决
2 回答2.6k 阅读✓ 已解决
3 回答5.1k 阅读✓ 已解决
3 回答1.8k 阅读✓ 已解决
翻下文档就能解决的事情