-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Flow-analysis involving callbacks should consider readonly fields #37273
Comments
But that's not always the case. You could have just as easily have this code:
Making sure this is not the case would involve following all code paths. (#9998) |
Interesting. I tried your I am surprised by this. I would expect typescript to reject the call to What it is doing is silently converting a |
|
Right, but in this case |
The proposed behavior would make this crashing code legal: function fn() {
const obj = { x: 10 as number | undefined };
const objView: Readonly<typeof obj> = obj;
return {
clear() {
obj.x = undefined;
},
objView
};
}
const a = fn();
if (a.objView.x) {
window.setTimeout(() => {
console.log(a.objView.x.toExponential());
});
a.clear();
} |
Thank you for the example, that makes sense. I guess I would say that this line should cause an error: const objView: Readonly<typeof obj> = obj; I understand that TypeScript has already decided that this is the way things should work. I would just like to voice my wish for a future where we can have an option for working with immutable types and values in TypeScript in a simple, intuitive and ergonomic fashion. I will be following #14909 and hope that progress is made. |
I think it should be unsurprising that |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
I imagine that this surely has been discussed previously but I searched thoroughly and couldn't find anything.
I am using TypeScript 3.8.3, with all of the strict options enabled.
I understand that if the "x" field is a mutable field, then the compiler is correct (it is possible for "x" to change to null at some point in the future). But I have declared it "readonly" and therefore "a.x" will always be non-null in the call to "printNum" and the code is always type-safe.
The text was updated successfully, but these errors were encountered: