mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-09 16:39:46 -05:00
Add more tests
This commit is contained in:
@@ -77,4 +77,23 @@ function foo(x: A | undefined) {
|
||||
x; // A
|
||||
}
|
||||
x; // A
|
||||
}
|
||||
|
||||
// X is neither assignable to Y nor a subtype of Y
|
||||
// Y is assignable to X, but not a subtype of X
|
||||
|
||||
interface X {
|
||||
x?: string;
|
||||
}
|
||||
|
||||
class Y {
|
||||
y: string;
|
||||
}
|
||||
|
||||
function goo(x: X) {
|
||||
x;
|
||||
if (x instanceof Y) {
|
||||
x.y;
|
||||
}
|
||||
x;
|
||||
}
|
||||
31
tests/cases/compiler/discriminantsAndTypePredicates.ts
Normal file
31
tests/cases/compiler/discriminantsAndTypePredicates.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
// Repro from #10145
|
||||
|
||||
interface A { type: 'A' }
|
||||
interface B { type: 'B' }
|
||||
|
||||
function isA(x: A | B): x is A { return x.type === 'A'; }
|
||||
function isB(x: A | B): x is B { return x.type === 'B'; }
|
||||
|
||||
function foo1(x: A | B): any {
|
||||
x; // A | B
|
||||
if (isA(x)) {
|
||||
return x; // A
|
||||
}
|
||||
x; // B
|
||||
if (isB(x)) {
|
||||
return x; // B
|
||||
}
|
||||
x; // never
|
||||
}
|
||||
|
||||
function foo2(x: A | B): any {
|
||||
x; // A | B
|
||||
if (x.type === 'A') {
|
||||
return x; // A
|
||||
}
|
||||
x; // B
|
||||
if (x.type === 'B') {
|
||||
return x; // B
|
||||
}
|
||||
x; // never
|
||||
}
|
||||
Reference in New Issue
Block a user