-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Overloading function fails with union types #29969
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This isn't actually related to strict null checks, but related to overload resolution. Here is the same problem without optionality and undefined. declare const value: "L" | "R";
function doSthWithValue(v: "L"): "L";
function doSthWithValue(v: "R"): "R";
function doSthWithValue(v: "L" | "R") {
return v;
}
// Argument of type '"L" | "R"' is not assignable to parameter of type '"R"'.
const a = doSthWithValue(value); I think the current situation is that TypeScript looks to prune overloads by only looking at the overloads that match the input type. If no overload individually matches then you get a type error. Basically, TypeScript is missing the following distributive rule for intersection types:
|
For some reason it worked when disabling Should I change the name of the issue? To what? |
Managed to track down what looks to be the canonical issue for this problem: #14107. |
For my case the following is enough: function doSthWithValue(v: Data): Data;
- function doSthWithValue(v: undefined): undefined;
+ function doSthWithValue(v: Data | undefined): Data | undefined;
function doSthWithValue(v?: Data) {
return v;
} Yet the issue is still current. Closing in favour of #14107 with hope for getting it fixed soon |
EDIT: see comment below, it is not connected to
strictNullChecks
Expected behavior:
Should not throw error with
strictNullChecks
enabledActual behavior:
It throws error:
Playground Link:
Enable
strictNullChecks
. The order of overloaded signatures does not change anythinghttps://www.typescriptlang.org/play/#src=interface%20Data%20%7B%0D%0A%20%20%20%20data%3A%20any%0D%0A%7D%0D%0A%0D%0Adeclare%20const%20value%3A%20Data%20%7C%20undefined%3B%0D%0A%0D%0Afunction%20doSthWithValue(v%3A%20undefined)%3A%20undefined%3B%0D%0Afunction%20doSthWithValue(v%3A%20Data)%3A%20Data%3B%0D%0Afunction%20doSthWithValue(v%3F%3A%20Data)%20%7B%0D%0A%20%20%20%20return%20v%3B%0D%0A%7D%0D%0A%0D%0Aconst%20a%20%3D%20doSthWithValue(value)%3B%0D%0A
Related issues:
#14107
The text was updated successfully, but these errors were encountered: