class C {
private p: string;
}
var strOrC: string | C;
var c: C;
// A type guard of the form typeof x === s,
// where s is a string literal with any value but 'string', 'number' or 'boolean',
// - when true, removes the primitive types string, number, and boolean from the type of x, or
// - when false, has no effect on the type of x.
if (typeof strOrC === "Object") {
c = strOrC; // C
} else {
var r2: string = strOrC; // string | C, 2322 ERROR
}
If strictNullCheck is false
class C {
private p: string;
}
var strOrC: string | C;
var c: C;
// A type guard of the form typeof x === s,
// where s is a string literal with any value but 'string', 'number' or 'boolean',
// - when true, removes the primitive types string, number, and boolean from the type of x, or
// - when false, has no effect on the type of x.
if (typeof strOrC === "Object") {
c = strOrC; // C
} else {
var r2: string = strOrC; // string
}
If strictNullCheck is true
If strictNullCheck is false
releated issue
1005 - https://github.com/dudykr/stc/issues/1005#issuecomment-1660741404
test case url
https://github.com/dudykr/stc/blob/main/crates/stc_ts_type_checker/tests/conformance/expressions/typeGuards/typeGuardOfFormTypeOfOther.ts