My scenario involves a static dictionary:
const myDict = {
1: "one",
2: "two"
}
The default inferred type in this case is Record<1 | 2, string>
.
I am seeking to create a type that exclusively accepts the exact string literals assigned to the properties in myDict
:
type T = {
1: typeof "one",
2: typeof "two"
}
How can I achieve this type derivation? My preference is to type the original dictionary so that typeof myDict
aligns with my desired type.
I am aware that I can assert the type using the as
keyword:
const myDict = {
1: "one" as typeof "one",
2: "two" as typeof "two"
}
However, this workaround is not practical and is error-prone when dealing with extensive dictionaries. Is there a more efficient approach I can take?