![](/img/trans.png)
[英]Is there a way to hint the passed argument type to IDE (in my case Webstorm) in Javascript?
[英]How to type hint specific objects in function argument
假设我有一些功能:
const someFunc = (input) => {// do something}
我知道输入是一个具有属性thing
的对象,即。 在函数中,我可能会检查input.thing
是什么。 声明函数时如何键入提示函数? 我试过类似的东西:
const someFunc = (input: { thing: string }) => {// do something}
但这似乎不起作用。
这工作得很好。 您正在声明第一个参数是一个对象,其中thing
是必需的字符串。
const someFunc = (input: { thing: string }) => {
}
使用类型而不是内联来解决它会很有帮助,因为内联类型看起来有点像解构。
type Input = {
thing: string
}
const someFunc = (input: Input) => {}
您可能希望允许其他键,但需要在这种thing
下您可以添加[key: string]: any
到您的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.