Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
3x071c opened this issue Feb 22, 2024 · 4 comments · Fixed by #57549
Assignees
Labels
Bug A bug in TypeScript

Comments

@3x071c
Copy link

3x071c commented Feb 22, 2024

🔎 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

@RyanCavanaugh
Copy link
Member

Isn't the bug earlier? The type { [K in keyof Partial<Obj>]: Obj[K] } should be { a: number, b: number } and the initialization from { } should fail.

@3x071c
Copy link
Author

3x071c commented Feb 22, 2024

@RyanCavanaugh The mapped definition seems to work as intended ({ a?: number, b?: number }), as it does in all minor TypeScript versions since 3.3.3 (from the playground). Kinda surprised if that actually isn't expected behavior.

@RyanCavanaugh
Copy link
Member

Bisects to #47109

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Feb 22, 2024
@jcalz
Copy link
Contributor

jcalz commented Feb 22, 2024

From here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
4 participants