mirror of
https://github.com/hanzoai/tsgo.git
synced 2026-10-30 19:37:15 +00:00
63 lines
1.4 KiB
TypeScript
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
|
|
}
|