We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
TypeScript narrows type wrong when using instanceof for generic class with optional field
typescript instanceof template optional narrowing
It seems the bug present in all versions: old, actual and nightly
https://www.typescriptlang.org/play?target=8&ts=4.8.0-dev.20220602#code/MYGwhgzhAECiA8sB80DeAoaXoFMD8AXHANzoC+66okMAgvAJIo4AeALjgHYAmMCTGbNABmASxwhuRTgFcAtgCMcAJ2gBeaAAZSFKuCjRaARkbN2XXodNpM2MRO5HC0WYpU7K1A7QBM11hw8dNaCduKSRtLySqoaRh7owjKcwGyiAPacIqYAFGBE9K4xSACUNkKiwtB50KKcEGxgKTjpVcZloUJYYAB09hGkQhQVVTV1DU3ALW0+HbZd0L39joPYFBRAA
class E<E> { e?: E; } class A<I> extends E<I>{ field: number = 0; } class A1<I> extends A<I> { field1?: number; } class A2<I> extends A<I> { field1: number = 1; } function f<I>(a: A<number>) { if (a instanceof A1) { a.field1; } if (a instanceof A2) { a.field1; } }
Property 'field1' does not exists in type A in first if, variable a narrowed to A
variable a narrowed to A1 in first if and property field1 exists
The text was updated successfully, but these errors were encountered:
Simplified
class Base<I> { e?: I; } class A1<I> extends Base<I> { field_A1?: number; } class A2<I> extends Base<I> { field_A2!: number; } function f(a: Base<number>) { if (a instanceof A1) { a.field_A1; // Not present, should be } if (a instanceof A2) { a.field_A2; // Correctly present } }
I'm kind of shocked this has been present since 3.3.3 with (AFAIK) no other reports
Sorry, something went wrong.
No branches or pull requests
Bug Report
TypeScript narrows type wrong when using instanceof for generic class with optional field
🔎 Search Terms
typescript instanceof template optional narrowing
🕗 Version & Regression Information
It seems the bug present in all versions: old, actual and nightly
⏯ Playground Link
https://www.typescriptlang.org/play?target=8&ts=4.8.0-dev.20220602#code/MYGwhgzhAECiA8sB80DeAoaXoFMD8AXHANzoC+66okMAgvAJIo4AeALjgHYAmMCTGbNABmASxwhuRTgFcAtgCMcAJ2gBeaAAZSFKuCjRaARkbN2XXodNpM2MRO5HC0WYpU7K1A7QBM11hw8dNaCduKSRtLySqoaRh7owjKcwGyiAPacIqYAFGBE9K4xSACUNkKiwtB50KKcEGxgKTjpVcZloUJYYAB09hGkQhQVVTV1DU3ALW0+HbZd0L39joPYFBRAA
💻 Code
🙁 Actual behavior
Property 'field1' does not exists in type A in first if, variable a narrowed to A
🙂 Expected behavior
variable a narrowed to A1 in first if and property field1 exists
The text was updated successfully, but these errors were encountered: