-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Open
Labels
Domain: check: Type InferenceRelated to type inference performed during signature resolution or `infer` type resolutionRelated to type inference performed during signature resolution or `infer` type resolutionHelp WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone
Description
π Search Terms
map overload any unknown unsafe weakmap set binary assignment lazy init
π Version & Regression Information
- This is the behavior in every version I tried
β― Playground Link
π» Code
let cache: Map<string, object> | undefined;
function test1(key: string) {
const result = (cache ??= new Map()).get(key);
// ^? const result: any
return result;
}
function test2(key: string) {
cache ??= new Map();
const result = cache.get(key);
// ^? const result: object | undefined
return result;
}π Actual behavior
First result gets spoiled as any
π Expected behavior
I'd expect both results to be object | undefined
Additional information about the issue
This bug reports an issue related to Map but, tbf, it's not unique to Map and a more general solution would have to be found to properly fix this. The problem is how checkBinaryLikeExpressionWorker create a union of left and right types here - without taking into consideration that the assignment target might already overlap with the right type. That union type gets reduced with UnionReduction.Subtype and .get gets read from it - instead of the assigned to reference on the left side.
nmain and whzx5byb
Metadata
Metadata
Assignees
Labels
Domain: check: Type InferenceRelated to type inference performed during signature resolution or `infer` type resolutionRelated to type inference performed during signature resolution or `infer` type resolutionHelp WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases