Issue
I'm looking to create a object type UiPrefixed
where all the keys in the object have to be prefixed with /^ui\:.+/
, is this possible?
type UiPrefixed = {
[`ui:*`]: any
}
const e: UiPrefixed = {
'ui:meow': true,
'ui:woof': true
}
Solution
Yes. This is one of the scenarios where template literal types can come in handy: ui:${string}
:
type UiPrefixed = {
[key: `ui:${string}`]: any
}
const e: UiPrefixed = {
'ui:meow': true,
'ui:woof': true
}
See proof-of-concept on TypeScript Playground.
Answered By - Terry
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.