TypeScript/tests/baselines/reference/typeOfThisInstanceMemberNarrowedWithLoopAntecedent.errors.txt
Song 8a05707559
Fix 31995: make cached key more precise to avoid returning wrong cached value. (#39670)
* fix 31995

* revert useless change only for debug.

* add test
2020-07-22 12:26:17 -04:00

39 lines
1.3 KiB
Plaintext

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'.
}
}