Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

// 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”。

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
585 views
Welcome To Ask or Share your Answers For Others

1 Answer

翻下文档就能解决的事情

type Bar = string;
type foo = () => Bar;
type baz = ReturnType<foo>;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...