microsoft / microsoft/TypeScript

Index signatures in type definitions: now we have `noUncheckedIndexedAccess`, include or omit `undefined`?

Open
#42,810 6 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

lib Update Request

Before we had noUncheckedIndexedAccess, it was common practice to add undefined to an index signature as a workaround. For example:

However, it seems this workaround wasn't extended to all index signatures, e.g.
RegExpMatchArray['groups'] in lib.es2018.regexp.d.ts and PropertyDescriptorMap in lib.es5.d.ts.

Now we have noUncheckedIndexedAccess I would like to ask what the best way forward is regarding these workarounds? For people who enable noUncheckedIndexedAccess, these workarounds are redundant. For people who don't enable noUncheckedIndexedAccess, these workarounds are still necessary—but potentially not always desirable since it forces a higher level of strictness.

It seems inconsistent that some index signatures use this workaround and others do not, so I think we should decide either to use this workaround in all index signatures or remove it from all index signatures and suggest people use noUncheckedIndexedAccess.

Including undefined creates a few problems, for example:

declare const v: DOMStringMap;

Object.values(v).map((value) => {
    // Expected: string;
    // Actual: string | undefined; ❌
    value;
});

for (const value of Object.values(v)) {
    // Expected: string;
    // Actual: string | undefined; ❌
    value;
}

const test: DOMStringMap = {
    // This should error
    foo: undefined
}

If the TypeScript team can decide the best way forward then this will also provide some guidance to the community—for example we're trying to decide what the best option is for @types/node: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/51177.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the index signatures in lib.es2018.regexp.d.ts, lib.es5.d.ts, and lib.dom.d.ts, then review the issue’s examples involving noUncheckedIndexedAccess. The issue does not specify which policy to adopt, so completion would require a team decision before updating the affected definitions and checking its impact on the cited behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.