Open
Description
TypeScript Version: 3.1
Search Terms: type inference, type guard, narrowing, lost, assignment
Code
let a: unknown = 'x';
if (typeof a === 'string') {
// a inferred as `string`
a = a.substr(0, 5); // (method) String.substr(from: number, length?: number): string
// a inferred as `unknown`
a.length; // Failure: Object is of type 'unknown'.
}
Expected behavior: This should compile without an error.
Actual behavior: Line 7 fails with: Object is of type 'unknown'.