![](/img/trans.png)
[英]How can I map values of object keys to the same object in TypeScript?
[英]How can I make a typescript type where all keys in object have same prefix?
我想創建一個 object 類型的UiPrefixed
,其中 object 中的所有鍵都必須以/^ui\:.+/
為前綴,這可能嗎?
type UiPrefixed = {
[`ui:*`]: any
}
const e: UiPrefixed = {
'ui:meow': true,
'ui:woof': true
}
是的。 這是模板文字類型可以派上用場的場景之一: ui:${string}
:
type UiPrefixed = {
[key: `ui:${string}`]: any
}
const e: UiPrefixed = {
'ui:meow': true,
'ui:woof': true
}
請參閱TypeScript Playground上的概念驗證。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.