microsoft / microsoft/TypeScript
Generic indexed access on type with optional properties doesn't include `undefined` in its constraint in `exactOptionalPropertyTypes`
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
exactOptionalPropertyTypes optional generic indexed access undefined missing contextual constraint eopt
🕗 Version & Regression Information
- This changed in 5.3.0-dev.20230914
⏯ Playground Link
💻 Code
// @exactOptionalPropertyTypes: true
interface XStateConfiguration {
theme?: "auto" | "dark" | "light";
viewColumn?: "beside" | "active";
}
declare function getVscodeConfiguration<T>(section: string): T | undefined;
export function getConfiguration<Name extends keyof XStateConfiguration>(
name: Name,
): XStateConfiguration[Name] {
const genericAssignabilityTest: XStateConfiguration[Name] = undefined; // error
return getVscodeConfiguration(name); // error
}
const concreteAssignabilityTest: XStateConfiguration[keyof XStateConfiguration] = undefined; // ok
🙁 Actual behavior
undefined is not assignable to an indexed access like this in expression space
🙂 Expected behavior
I'd expect this to typecheck OK - just like the concrete example
Additional information about the issue
No response
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 with the linked TypeScript Playground repro using exactOptionalPropertyTypes and compare the generic indexed-access assignments with the concrete indexed access. Trace the type-checking path for XStateConfiguration[Name] in expression space and verify that undefined is accepted consistently in both assignments and the function return.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100