microsoft / microsoft/TypeScript

Types with optional props get implicit index signatures that cannot be made explicit

Open
#59,392 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms

implicit index signatures, explicit index signatures, optional partial undefined,

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about index signatures and undefined
⏯ Playground Link

Playground link

💻 Code
// @strict: true
// @exactOptionalPropertyTypes: false
// @noUncheckedIndexedAccess: false

type ObjImplicit = {
    a?: string | undefined,
    b: string
};
type Idx = { [k: string]: string }
declare const o: ObjImplicit;
const i: Idx = o; // allowed because A has implicit index signature

type ObjExplicit = {
    a?: string | undefined, // error! not allowed
    //~ <-- Property 'a' of type 'string | undefined' is not assignable to 'string' index type 'string'.
    b: string,
    [k: string]: string
}
🙁 Actual behavior

{ a?: string | undefined } can implicitly have the index signature {[k: string]: string} but cannot explicitly have such an index signature

🙂 Expected behavior

I think I expected the implicit index signature to fail?

Additional information about the issue

Without --exactOptionalPropertyTypes, {a?: string} and {a?: string | undefined} are the same type. I figured that would mean both implicitly and explicitly adding index signatures would either require or not require undefined.

We also have the situation where Partial<{[k: string]: string}> becomes {[k: string]: string | undefined} instead of {[k: string]: string}, which makes me lean toward "require undefined", for consistency. Currently the discrepancy leads to questions like this one.

Anyway, I expect this to not be a bug, but intended behavior that flew under my radar and which I cannot see explicitly documented anywhere. Mostly I just want to understand this behavior so I can explain it when people ask questions like the one linked above.

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 with the linked TypeScript Playground and compare the implicit and explicit index-signature examples under the shown compiler options. Trace the relevant type-checking behavior to determine the intended rule, then document that rule and the relationship with exactOptionalPropertyTypes and Partial; done means the discrepancy is clearly explained.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.