![](/img/trans.png)
[英]How to define a function's argument type as one of the keys (or properties) of an interface
[英]define type to equal one of the interface properties
假設我們有一個接口:
interface Foo {
aaa: string
bbb: SomeType
ccc: SomeOtherType
// ... and 100 more
}
我想定義一個類型,以便它可以接收任何Foo
接口屬性類型。
顯然我可以手動完成:
type Bar = Foo['aaa'] | Foo['bbb'] // etc.
但這是不切實際的,也不是 DRY,尤其是對於大接口。 有沒有更好的方法來定義這種類型,以便它自動接受接口類型?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.