-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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 wrongly excludes undefined with optional properties #44494
Comments
This is an intentional consistency change, since it's legal to initialize const a: { b?: 1 } = { b: undefined };
const c = Object.values(a);
console.log(c); However, this is a use case I'd expect to be handled properly with the new |
It's legal to initialize Same issue with |
Well, that's why we added the |
What is exactly this flag ? Is it yet available ? I can't find it in the doc... |
It's new (as in, unreleased); see #43947 |
Thanks. I'm not sure that this tag will solve the issue. It will still be possible to allow |
?. The entire purpose of the flag is to make that not be allowed. |
In my understanding, its purpose is to allow (not to force!) an optional property to never be |
This is an error under strictOptionalProperties: let x: { foo?: string } = { foo: undefined }; You either have to give |
Yes but that is not an error:
And this is the repro for my issue... |
The cause of this change is #43086. It's debatable what the correct behavior is, as |
But thereβs no index signature in the reproβ¦ |
There's actually an index signature in the type of TypeScript/lib/lib.es2017.object.d.ts Line 26 in 5540364
I think it is understandable to strip |
Don't forget that any external module can choose to export something like |
So, there are a few things we should definitely fix:
This will make the current definitions of That said, I'm a bit surprised we have strongly typed definitions for In addition, if we stick with strongly typed values<T>(obj: T): T extends readonly any[] ? T[number][] : Required<T>[keyof T][];
entries<T>(obj: T): T extends readonly any[] ? [string, T[number]][] : [string, Required<T>[keyof T]][]; |
@DanielRosenwasser Opinions? |
The "improvements" to those APIs were incorrectly merged at some point and now it's too much of a break to take them out. Maybe we should just give up on The inference rule changes seem both correct and necessary for SOP. |
With #44595, the original example behaves as expected in I'm leaving this issue open to track possible changes to |
Thanks a lot! |
The key/value/entries issue is tracked at #38520, so closing this |
Mentioning |
Bug Report
π Search Terms
object values entries undefined optional properties
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
c
has type1[]
but can be equal to[undefined]
π Expected behavior
c
should have type(undefined | 1)[]
The text was updated successfully, but these errors were encountered: