mirror of
https://github.com/hanzoai/tsgo.git
synced 2026-10-30 19:37:15 +00:00
45 lines
787 B
TypeScript
45 lines
787 B
TypeScript
//// [tests/cases/conformance/types/nonPrimitive/nonPrimitiveNarrow.ts] ////
|
|
|
|
//// [nonPrimitiveNarrow.ts]
|
|
class Narrow {
|
|
narrowed!: boolean
|
|
}
|
|
|
|
declare var a: object;
|
|
|
|
if (a instanceof Narrow) {
|
|
a.narrowed; // ok
|
|
a = 123; // error
|
|
}
|
|
|
|
if (typeof a === 'number') {
|
|
a.toFixed(); // error, never
|
|
}
|
|
|
|
declare var b: object | null;
|
|
|
|
if (typeof b === 'object') {
|
|
b.toString(); // ok, object | null
|
|
} else {
|
|
b.toString(); // error, never
|
|
}
|
|
|
|
|
|
//// [nonPrimitiveNarrow.js]
|
|
"use strict";
|
|
class Narrow {
|
|
}
|
|
if (a instanceof Narrow) {
|
|
a.narrowed; // ok
|
|
a = 123; // error
|
|
}
|
|
if (typeof a === 'number') {
|
|
a.toFixed(); // error, never
|
|
}
|
|
if (typeof b === 'object') {
|
|
b.toString(); // ok, object | null
|
|
}
|
|
else {
|
|
b.toString(); // error, never
|
|
}
|