Files
tsgo/testdata/baselines/reference/submodule/compiler/discriminantOrderIndependence.js

63 lines
1.4 KiB
TypeScript

//// [tests/cases/compiler/discriminantOrderIndependence.ts] ////
//// [discriminantOrderIndependence.ts]
interface A {
subType: "b";
type: "a";
}
declare let order1:
| { type: "1" }
| A
| { type: "2" }
| { type: "3" }
| undefined;
// Should NOT error: 'order1' is possibly 'undefined' after the guard
if (order1 && order1.type === "a") {
order1.type; // Should be OK
}
interface B {
subType: "b";
type: "a";
}
declare let order2:
| { type: "1" }
| { type: "2" }
| { type: "3" }
| B
| undefined;
// Should NOT error: 'order2' is possibly 'undefined' after the guard
if (order2 && order2.type === "a") {
order2.type; // Should be OK
}
// Also test with !. type assertion
if (order1 && order1.type === "a") {
order1.type; // Should be OK
}
if (order2 && order2.type === "a") {
order2.type; // Should be OK
}
//// [discriminantOrderIndependence.js]
"use strict";
// Should NOT error: 'order1' is possibly 'undefined' after the guard
if (order1 && order1.type === "a") {
order1.type; // Should be OK
}
// Should NOT error: 'order2' is possibly 'undefined' after the guard
if (order2 && order2.type === "a") {
order2.type; // Should be OK
}
// Also test with !. type assertion
if (order1 && order1.type === "a") {
order1.type; // Should be OK
}
if (order2 && order2.type === "a") {
order2.type; // Should be OK
}