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

Object.values does not return an undefined type when used on a partial record with explicit string keys set #54049

Closed
etler opened this issue Apr 28, 2023 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@etler
Copy link

etler commented Apr 28, 2023

Bug Report

When callign Object.values on a partial record with an explicit literal string key set it does not return a union with undefined as expected. This improper type inference can lead to runtime errors by accessing undefined properties.

🔎 Search Terms

"Object.values Partial"

🕗 Version & Regression Information

  • This changed between versions 4.2.3 and 4.3.5

In the playground, the behavior was correct in version 4.2.3

It first broke in 4.3.5 and still exists in 5.0.4 and nightly.

(I have not tested versions in between 4.2.3 and 4.3.5 as they were not easily available to test in the playground).

⏯ Playground Link

Playground link with relevant code

💻 Code

/* Incorrect Behavior */
type Foo = Partial<Record<"key", {count: number}>> // Partial Records allow for values to be missing or undefined
const foo: Foo = {key: undefined} // Value is explicitely set to be undefined
const valuesFoo = Object.values(foo) // Type is of {count: number}[]
console.log(valuesFoo.map((value) => value.count)) // Runtime error but no type check error

/* Correct Behavior */
type Bar = Partial<Record<string, {count: number}>> // Record changed to have string primitive instead
const bar: Bar = {key: undefined} // Value is explicitely set to be undefined
const valuesBar = Object.values(bar) // Type is of ({count: number} | undefined)[]
console.log(valuesBar.map((value) => value.count)) // Error is caught by type checker

🙁 Actual behavior

Object.values did not return a union with undefined. This is incorrect because the return value can be undefined.

🙂 Expected behavior

Object.values should return a union with undefined. This is expected because the return value can be undefined.

@jcalz
Copy link
Contributor

jcalz commented Apr 29, 2023

Duplicate of #44494 and therefore being tracked at #38520

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 2, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants