Closed
Description
When you refine a variable with a user defined type guard, and then reassign that variable to the result of a function returning the same type, TS loses track of the refined type.
Search Terms:
refinement
Code
interface A { _a: true }
const isA = (item: any): item is A => true;
declare const foo: any;
function example<T extends {}>(input: T): T {
return input;
}
function exampleUsage<T extends {}>(item: T) {
if (isA(item)) {
// works
console.log(item._a);
}
item = example(item);
if (isA(item)) {
// works
console.log(item._a);
}
if (isA(item)) {
item = example(item);
console.log(item._a);
}
}
Expected behavior:
In the final case, because the function example
returns the same type as it was provided, TS should keep track of item's refined type.
Actual behavior:
TS loses track of the fact that item
has previously been refined to an instance of A
.
Related Issues: n/a