【发布时间】:2019-03-25 13:49:08
【问题描述】:
如何将函数名输入为字符串?这个伪代码说明了我的问题
const foo = () => null;
const bar = () => null;
interface Sth {
caller: nameof foo | nameof bar
}
以上代码产生如下错误:
error TS2503: Cannot find namespace 'bar'.
这样就可以了:
const x: Sth = { caller: 'foo' }
但这会出错:
const y: Sth = { caller: 'y' }
【问题讨论】:
-
那些是匿名函数...匿名函数没有
name属性。我的意思是,这些是存储在名为foo和bar的变量中的匿名函数,但函数本身没有名称。 -
这个问题一团糟。不仅难懂意思,甚至连OK格式都没有,这是对读者的不尊重。你最好换个新的尝试,问新的,格式更好的问题。
-
你有什么不明白的?如果您对格式有一些建议,请随时编辑问题。
标签: typescript