Microsoft/TypeScript
在 GitHub 查看Strange narrowing when using instanceof on template class with optional field
Open
#49,352 建立於 2022年6月2日
BugDomain: check: Control FlowHelp Wanted
倉庫指標
- Star
- (48,455 star)
- PR 合併指標
- (平均合併 6天 17小時) (30 天內合併 9 個 PR)
描述
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
💻 Code
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;
}
}
🙁 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