-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
Milestone
Description
Bug Report
π Search Terms
type guard get property const
π Version & Regression Information
Fails at least on 4.3.5 and 4.4.0-beta.
- I was unable to test this on prior versions because I noticed this behavior very recently.
β― Playground Link
Playground link with relevant code
π» Code
const x: string | number = Math.random() < 0.5 ? "str" : 123;
if (typeof x === "string") {
let obj = {
get prop() { return x.toUpperCase() }, // Error: property toUpperCase doesn't exist on number
method() { return x.toUpperCase() } // Ok
}
}π Actual behavior
The method compiles, the getter raises an error.
π Expected behavior
Behaviour should be the same for both.
Given that x is a const and its type was narrowed to string, no error seems right.