Description
Inspiration is from this article https://levelup.gitconnected.com/building-type-safe-dictionaries-in-typescript-a072d750cbdf as well as practical problem that we encountered in our codebase.
If we want a type-safe dictionary where key is e.g. string we can simply use a Record:
const x: Record<string, number> = {}However, if we want to restrict set of keys:
type Key = 'A' | 'B' | 'C'Then suddenly TypeScript requires all keys to be present:
// Does not compile
const x: Record<Key, number> = {}The only solution seems to be to allow undefined values with Partial:
const x: Partial<Record<Key, number>> = {}However, this now breaks Object.values and in general it's not good because the type of value becomes number | undefined, which is not what we want.
Another issue is with Object.entries and Object.keys which are not type-safe either.
Suggestion
Support first-class type-safe dictionaries where set of keys is finite:
const x: Dict<Key, number> = {}and provide type-safe Object.keys, Object.values and Object.entries.
Dict says that:
- Keys must be of type
K. - Not all keys may be present.
- Type of value is
V.
Note that this is only about type-safety, Dict is still a plain JavaScript object.
Activity
MartinJohns commentedon Jan 25, 2021
This would be covered by #26797.
typescript-bot commentedon Jan 29, 2021
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
intgr commentedon Nov 5, 2021
This was marked as duplicate of #26797, which was instead superseded by #44512, released in TypeScript 4.4.
But this original issue remains unsolved in TypeScript 4.4. There is no indication that accessing attributes of
Record<>may returnundefinedfor nonexistant keys. And there is noDict<>type as proposed in this issue.Example: https://www.typescriptlang.org/play?#code/MYewdgzgLgBAtgQwA4C4YCUCmoBOATAHmhwEswBzAGhmLPID4YBeGAbwF8BuAKG9ElgkomOGloVm8ZAG0ARGHCYAHiWgIwUWQF1OMGAHp9MAMoALEAFcANnhgAjTDEw4cIHNQAGQkR5gA3BCsLR1UYDwswPEwAMzJMPA9uIA
Please re-open. Or redirect to another similar issue.
intgr commentedon Nov 5, 2021
Update: This behavior can be requested with
--noUncheckedIndexedAccessin TypeScript 4.1+Doc: https://www.typescriptlang.org/tsconfig#noUncheckedIndexedAccess
Related: #13778 #39560