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
Apologies, this doesn't look like a regression, but I couldn't find this in any of the FAQs. If this is a duplicate or a 'won't do' can someone point me to it?
typeSomeObject={userType: "admin"|"user";}functionusesAnObject(obj: SomeObject){}consttheObject5=Object.freeze({userType: "admin"});usesAnObject(theObject5);// Argument of type 'Readonly<{ userType: string; }>' is not assignable to parameter of type 'SomeObject'.
🙁 Actual behavior
In this code the type of the userType property is widened to string.
🙂 Expected behavior
The type should be kept as the narrower "admin" as it is not possible for that property to be reassigned.
The text was updated successfully, but these errors were encountered:
dwjohnston
changed the title
Object.freeze should derive narrow types
Object.freeze should infer narrow types on immediate properties
Aug 6, 2021
If we had #30680 then the signature to Object.freeze() could use it; without it, there are dirty tricks that could work, but I don't think anyone wants to see them in the standard TypeScript library.
Bug Report
🔎 Search Terms
Object.freeze narrow string
🕗 Version & Regression Information
Apologies, this doesn't look like a regression, but I couldn't find this in any of the FAQs. If this is a duplicate or a 'won't do' can someone point me to it?
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
In this code the type of the
userType
property is widened tostring
.🙂 Expected behavior
The type should be kept as the narrower
"admin"
as it is not possible for that property to be reassigned.The text was updated successfully, but these errors were encountered: