Skip to content

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

Closed
semyonf opened this issue Jun 2, 2021 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@semyonf
Copy link

semyonf commented Jun 2, 2021

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

function inner(flag: true): number;
function inner(flag: false): string;
function inner(flag: boolean): string | number {
    return flag ? 1 : 'A';
}

function outer(flag: true): number;
function outer(flag: false): string;
function outer(flag: boolean): string | number {
    return inner(flag);
}

Unless I throw in a conditional statement that effectively narrows the boolean flag into true and false inside respective branches.

function inner(flag: true): number;
function inner(flag: false): string;
function inner(flag: boolean): string | number {
    return flag ? 1 : 'A';
}

function outer(flag: true): number;
function outer(flag: false): string;
function outer(flag: boolean): string | number {
    if (flag) {
        return inner(flag);
    }

    return inner(flag);
}

πŸ™ Actual behavior

No overload matches this call.
  Overload 1 of 2, '(flag: true): number', gave the following error.
    Argument of type 'boolean' is not assignable to parameter of type 'true'.
  Overload 2 of 2, '(flag: false): string', gave the following error.
    Argument of type 'boolean' is not assignable to parameter of type 'false'.(2769)

πŸ™‚ Expected behavior

Compiles fine

@fatcerberus
Copy link

The implementation signature doesn’t count for purposes of overload resolution.

@MartinJohns
Copy link
Contributor

Duplicate of #40827.

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Jun 3, 2021
@DanielRosenwasser
Copy link
Member

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';
}

@behzadmehrabi
Copy link

Hi Guyz,
I have the following problem:

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?

@MartinJohns
Copy link
Contributor

@Bezmehrabi Repeat the overloads on your func function. Also, you better ask such questions on StackOverflow.

@behzadmehrabi
Copy link

behzadmehrabi commented Mar 14, 2022

@MartinJohns Thanks for answering.
Will ask the next one on stackoverflow πŸ‘Œ.

If the inner function is a utility function that's going to be used in so many func like functions, it will be so messy to have too many overloads in app.
Isn't there any other workaround?πŸ€”

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants