1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
tests/cases/conformance/classes/members/instanceAndStaticMembers/typeOfThisInstanceMemberNarrowedWithLoopAntecedent.ts(29,13): error TS2322: Type 'string | number' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.
==== tests/cases/conformance/classes/members/instanceAndStaticMembers/typeOfThisInstanceMemberNarrowedWithLoopAntecedent.ts (1 errors) ====
// #31995
type State = {
type: "numberVariant";
data: number;
} | {
type: "stringVariant";
data: string;
};
class SomeClass {
state!: State;
method() {
while (0) { }
this.state.data;
if (this.state.type === "stringVariant") {
const s: string = this.state.data;
}
}
}
class SomeClass2 {
state!: State;
method() {
const c = false;
while (c) { }
if (this.state.type === "numberVariant") {
this.state.data;
}
let n: number = this.state?.data; // This should be an error
~
!!! error TS2322: Type 'string | number' is not assignable to type 'number'.
!!! error TS2322: Type 'string' is not assignable to type 'number'.
}
}
|