-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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 removes undefined from optional properties #48587
Comments
The option exactOptionalPropertyTypes will produce your expect behavior. |
True, but the error should be reported even if |
TS doesn't differentiate between See #13195 |
@IllusionMH type TypeOptional = {
one?: string | undefined;
two?: string | undefined;
}
const typeOptMap: TypeOptional = {};
typeOptMap.one = undefined; // Here it's fine to assign `undefined`
for(const value of Object.values(typeOptMap)) {
console.log(value.length); // Here no error accessing the length as if value couldn't be undefined
} |
That's how it was implemented initially and changing that would break a lot of existing code. But for cases like yours new flag was implemented so you can enable it and see expected errors. What's the problem with flag? Just enable it ang get errors in your code base without breaking code of others. |
@IllusionMH type TypeOptional = {
one?: string | undefined;
two?: string | undefined;
}
const typeOptMap: TypeOptional = {};
'one' in typeOptMap && console.log(typeOptMap.one.length); // Object is possibly 'undefined'.
for(const value of Object.values(typeOptMap)) {
console.log(value.length); // no errors
} TS knows that the value is nullable when you directly access it, but not when you get it from |
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow or the TypeScript Discord community. |
Is there any possibility of having this show a warning by default atleast? I'm working in an existing codebase where enabling exactOptionalPropertyTypes would break a lot of things. I think there is room for improvement if the only options for this are to either:
TS is meant to allow us to "Catch errors early in your editor" which it does excellently but in this case its surprising the only way to get any indication of an issue is to make a potentially major change with some refactoring cost. I haven't tried it but I guess enabling exactOptionalPropertyTypes and making the type of all optional properties unions with Also @IllusionMH you mentioned "that's how it was implemented initially" however it seems this behaviour changed in v4.3, where it used to show undefined values when an object had optional properties, so is this not a regression? |
Would it be possible to allow Specifically, the flag is very problematic to use in React code, as it prevents passing along optional properties without jumping through hoops:
|
Bug Report
π Search Terms
enum index signature values
object values infer optional
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
In the following code type of values returned by
Object.values
is correctly infered tostring | undefined
:But if I add
?
to the properties, the type is nowstring
Real-world example is about using enums for indexing an object:
π Actual behavior
Values from properties with
?
and| undefined
are considered non-nullish.π Expected behavior
Error in all the cases
The text was updated successfully, but these errors were encountered: