dudykr / stc

Speedy TypeScript type checker
https://stc.dudy.dev
Apache License 2.0
5.76k stars 166 forks source link

[extra error] Type guard of form type of other should be filter string | number | boolean #1056

Open sunrabbit123 opened 1 year ago

sunrabbit123 commented 1 year ago

If strictNullCheck is true

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
}

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