[英]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.