-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Readonly on field causes control flow analysis to work strangely #13247
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
Comments
If typescript was doing a structural check, Birds are assignable as Fish. But if I remove the readonly keyword the error goes away and control flow works as expected. If I add more fields to Fish, then Bird is no longer assignable to Fish and the error goes away too. I don't understand why adding readonly would change how assignability works. |
Could it be related to this? class FileSystemObject {
isFile(): this is MyFile { return this instanceof MyFile; }
isDirectory(): this is Directory { return this instanceof Directory; }
}
class MyFile extends FileSystemObject {
name;
}
class Directory extends FileSystemObject {}
let fso: FileSystemObject;
if (fso.isFile()) {
fso; // fso is MyFile
}
else if (fso.isDirectory()) { // It works fine but if we remove name from MyFile fso become never
fso; // fso is Directory
} |
This works as expected now |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TypeScript Version: 2.1.4
Code
Attempted to compile this on the typescript playground and with tsc 2.1.4 on my mac.
This could just be my misunderstanding of how the
readonly
keyword works.Expected behavior:
x
in the first if block has typeFish
x
in the second block has typeBird
Actual behavior:
x
in the first block has typeFish | Bird
x
in the second block has typenever
error TS2339: Property 'name' does not exist on type 'never'.
The text was updated successfully, but these errors were encountered: