microsoft / microsoft/TypeScript
Index signatures in type definitions: now we have `noUncheckedIndexedAccess`, include or omit `undefined`?
Nobody has claimed this yet.
- 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:
- https://github.com/microsoft/TypeScript/issues/13792 in which the index signature inside
DOMStringMap(lib.dom.d.ts) was modified to includeundefined. - https://github.com/DefinitelyTyped/DefinitelyTyped/pull/43931 in which all index signatures in
@types/nodewere modified to includeundefined(e.g.ProcessEnvandParsedUrlQuery).
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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