[英]construct string union type from keys of custom type in TypeScript
如何在基於自定義類型的鍵的 TypeScript 中構造字符串聯合類型? 當然,只有當它們都是字符串時才有效。
假設我有這個:
type MyFields {
name: string
email: string
password: string
}
我怎么能產生這樣的東西:
type MyFieldsKeys = 'name' | 'email' | 'password'
有這樣的功能嗎?
It'a什么樣的相反的記錄呢-我可能例如產生MyFields
從MyfieldsKeys
與Record
做MyFields = Record<MyFieldsKeys, string>
使用keyof
生成鍵的字符串或數字文字聯合。
type MyFields = {
name: string
email: string
password: string
}
type MyfieldsKeys = keyof MyFields;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.