Skip to content

Regression: Indexed Access of Partial Mapped Type with Union from Generic Function is missing undefined #57487

Closed
@3x071c

Description

@3x071c

🔎 Search Terms

generic function, partial, optional, mapped type, indexed access, undefined, bug, regression

🕗 Version & Regression Information

  • This is a bug
  • This changed between versions 4.5.5 and 4.6.2
  • This is the behavior in every version I tried (all playground versions)

⏯ Playground Link

https://www.typescriptlang.org/play?target=99&jsx=0&pretty=true&ts=4.6.2#code/C4TwDgpgBA8gRgKygXigbwFBW1AhgLigEYAaLHOQgJgwF8BuDDAYwHsA7AZ2CgFtcwkACaFMOKAG0A0lACW7KAGsIIVgDMoABVwAnYLNwAbADzwEAPgC6hM9Mt0U6WkzZceOiJ1aGAbhACyAsKOxjIQAB7AEOxCnEoq6lCgkIn8ghBC5gAUyiCEUgCUKOZ8QRkSuZYAdMCsAMrAOvIA5lkFjAD0HVAAegC0fVAAojo6rDpQEZDMUUJQWQAGacIVKpYLcnFgrJycsnCGIFAArjEQavIZBRhAA

💻 Code

type Obj = {
    a: 1,
    b: 2
};

const mapped: {
    [K in keyof Partial<Obj>]: Obj[K]
} = {}

const resolveMapped = <K extends keyof typeof mapped>(key: K) => mapped[key].toString();
// ^-- Error expected (`mapped[key]` is possibly undefined)

🙁 Actual behavior

TypeScript does not recognize mapper[key] might be undefined and emits broken code without errors

🙂 Expected behavior

Correct type inference from pre-v4.6.x

Additional information about the issue

No response

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions