-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Unable to assign a boolean argument to a parameter that accepts both true and false #44392
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
The implementation signature doesnβt count for purposes of overload resolution. |
Duplicate of #40827. |
You can rewrite as function inner(flag: true): number;
function inner(flag: false): string;
function inner(flag: boolean): string | number
function inner(flag: boolean): string | number {
return flag ? 1 : 'A';
} |
Hi Guyz, function inner(flag: true): number;
function inner(flag: false): string;
function inner(flag: boolean): string | number
function inner(flag: boolean): string | number {
return flag ? 1 : 'A';
}
// works fine.
let r = inner(true)
function func(b: boolean) {
return inner(b)
}
// doesn't work as I want it, the return type is "string | number" but it should be "string"
let r2 = func(true) how can I fix that? |
@Bezmehrabi Repeat the overloads on your |
@MartinJohns Thanks for answering. If the inner function is a utility function that's going to be used in so many |
Uh oh!
There was an error while loading. Please reload this page.
Bug Report
π Search Terms
function overloads boolean not assignable to parameter of type true false
π Version & Regression Information
v4.4.0-dev.20210601
This is the behavior in every version I tried, and I reviewed the FAQ
β― Playground Link
Playground link with relevant code
π» Code
This will not compile
Unless I throw in a conditional statement that effectively narrows the boolean
flag
intotrue
andfalse
inside respective branches.π Actual behavior
π Expected behavior
Compiles fine
The text was updated successfully, but these errors were encountered: