You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Optional nested readonly type changes type of property adding undefined #55058
I was working with a library which reported that a property had an additional value of undefined, and the author of the plugin claimed that the error went by removing readonly. After investigating I found the following, which I found confusing.
The framework we use (vue) has a function for marking objects as deeply readonly:
The code above is transformed to the following types. The generated type for the variable user is incompatible with the exported class User, because it adds undefined as type to the type of the optional parameter of the class.
I assume the problem is that, for optional properties, regardless of eOPT, T[K] evaluates to P | undefined, because that's the type you will get if you read from that property. In other words:
constval: typeofobj[typeofkey]=obj[key];
which wouldn't hold if the undefined wasn't included for optional properties.
Oh! The generated types are different based on this option 🫣
Ryan Cavanaugh (@RyanCavanaugh) Ok, let me explain ... The example itself is a bit more complicated 😊 Some of the code is taken from a code-basis, where I have no control over - an external library. This external library generates the types and does not have exactOptionalPropertyTypes enabled.
I, in my project, want to have exactOptionalPropertyTypes enabled, which is why I am getting this error.
Here's the example. I don't know about vue, but the other plugin (Auth0Vue) has it disabled. This means, the example is best split into two:
Is this expected? Well, I would assume, that projects are independent by best effort. Now this project forces me to disable exactOptionalPropertyTypes for my project, if they do not enable it or change the code (as of now, they've merged changes for the latter by removing the readonly wrapping).
I can't really come up with a good solution here. A project would almost have to know with which parameters a set of types are built, right ..? 🙈
When I take my playground example, and flip the switch for exactOptionalPropertyTypes from true to false and back again, the only thing that changes is the property user.a, even though User.a also implicitly could be undefined, which is not stated in the generated types. This could is a type-insecurity when now using it in a project having exactOptionalPropertyTypes enabled.
Maybe I'm also just going way off track ... I'm just trying to find a good solution 😇
Activity
MartinJohns commentedon Jul 18, 2023
What is
exactOptionalPropertyTypesset to? If it's set tofalsethere's no difference between the types. The additionalundefinedmakes no difference.SimonSimCity commentedon Jul 18, 2023
Sorry, forgot to mention 😅 I've set
exactOptionalPropertyTypestotrue, as recommended.fatcerberus commentedon Jul 18, 2023
I assume the problem is that, for optional properties, regardless of
eOPT,T[K]evaluates toP | undefined, because that's the type you will get if you read from that property. In other words:which wouldn't hold if the
undefinedwasn't included for optional properties.RyanCavanaugh commentedon Jul 18, 2023
This behavior follows from the definition of the type, as Bruce Pascoe (@fatcerberus) points out.
Turning on EOPT, I see the desired behavior. Can you clarify?
SimonSimCity commentedon Jul 19, 2023
Oh! The generated types are different based on this option 🫣
Ryan Cavanaugh (@RyanCavanaugh) Ok, let me explain ... The example itself is a bit more complicated 😊 Some of the code is taken from a code-basis, where I have no control over - an external library. This external library generates the types and does not have
exactOptionalPropertyTypesenabled.I, in my project, want to have
exactOptionalPropertyTypesenabled, which is why I am getting this error.Here's the example. I don't know about vue, but the other plugin (Auth0Vue) has it disabled. This means, the example is best split into two:
Is this expected? Well, I would assume, that projects are independent by best effort. Now this project forces me to disable
exactOptionalPropertyTypesfor my project, if they do not enable it or change the code (as of now, they've merged changes for the latter by removing thereadonlywrapping).SimonSimCity commentedon Jul 19, 2023
I can't really come up with a good solution here. A project would almost have to know with which parameters a set of types are built, right ..? 🙈
When I take my playground example, and flip the switch for
exactOptionalPropertyTypesfromtruetofalseand back again, the only thing that changes is the propertyuser.a, even thoughUser.aalso implicitly could beundefined, which is not stated in the generated types. This could is a type-insecurity when now using it in a project havingexactOptionalPropertyTypesenabled.Maybe I'm also just going way off track ... I'm just trying to find a good solution 😇
fatcerberus commentedon Jul 19, 2023
Simon Schick (@SimonSimCity) This is a known pain point: see #54212 and #49886
typescript-bot commentedon Jul 23, 2023
This issue has been marked as "Question" and has seen no recent activity. It has been automatically closed for house-keeping purposes.