Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support type-safe dictionaries where set of keys is finite #42477

Closed
alamothe opened this issue Jan 25, 2021 · 4 comments
Closed

Support type-safe dictionaries where set of keys is finite #42477

alamothe opened this issue Jan 25, 2021 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@alamothe
Copy link

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.

@MartinJohns
Copy link
Contributor

This would be covered by #26797.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 26, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@intgr
Copy link

intgr commented 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 return undefined for nonexistant keys. And there is no Dict<> type as proposed in this issue.

Example: https://www.typescriptlang.org/play?#code/MYewdgzgLgBAtgQwA4C4YCUCmoBOATAHmhwEswBzAGhmLPID4YBeGAbwF8BuAKG9ElgkomOGloVm8ZAG0ARGHCYAHiWgIwUWQF1OMGAHp9MAMoALEAFcANnhgAjTDEw4cIHNQAGQkR5gA3BCsLR1UYDwswPEwAMzJMPA9uIA

Please re-open. Or redirect to another similar issue.

@intgr
Copy link

intgr commented Nov 5, 2021

Update: This behavior can be requested with --noUncheckedIndexedAccess in TypeScript 4.1+

Doc: https://www.typescriptlang.org/tsconfig#noUncheckedIndexedAccess

Related: #13778 #39560

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants