[英]How do you declare function params in typescript with an interface?
我知道在 TS 我可以做到:
const myFunc = (choice: string, index: number) => {}
但是我將如何使用那里的接口(我應該如何使用最佳實踐?)
我想做類似的事情
interface myFuncArgs {
choice: string
index: number
}
const myFunc = (choice, index): myFuncArgs => {}
但它不喜歡那樣
我知道對於這個簡單的案例,它並沒有真正的意義,但是如果choice
是一個復雜的對象並且我想聲明其中所有內容的類型怎么辦? 它會看起來丑陋的內聯。
我該怎么做呢?
你可以按照@YevgenGorbunkov 在評論中所說的去做:
const myFunc = ({choice, index}: myFuncArgs): void => {}
或者你甚至可以這樣做(如果你不喜歡傳遞一個對象):
type MyFunc = (choice : myFuncArgs['choice'], index: myFuncArgs['index']) => void
const myFunc : MyFunc = (choice, index) => {
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.