Skip to content

Type 'boolean' is not assignable to type 'true' with boolean being used by typescript for literal true. #24413

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
phorsuedzie opened this issue May 25, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@phorsuedzie
Copy link

TypeScript Version:
2.8.3
2.9.0-rc
3.0.0-dev.20180522

Search Terms:
true as true

Code

interface Good {
    ok: true,
    data: string,
}

interface Bad {
    ok: false,
}

type Result = Good | Bad;

function convertPromiseDoesntCompile(p: Promise<string>): Promise<Result> {
    return p.then(
        (data: string) => ({ ok: true, data }),
        () => ({ ok: false })
    );
}

function convertPromiseCompiles(p: Promise<string>): Promise<Result> {
    return p.then(
        (data: string) => ({ ok: true as true, data }),
        () => ({ ok: false as false})
    );
}

Expected behavior:

Compiles without the uselessly looking casts

Actual behavior:

Type 'Promise<{ ok: boolean; data: string; } | { ok: boolean; }>' is not assignable to type 'Promise<Result>'.
  Type '{ ok: boolean; data: string; } | { ok: boolean; }' is not assignable to type 'Result'.
    Type '{ ok: boolean; data: string; }' is not assignable to type 'Result'.
      Type '{ ok: boolean; data: string; }' is not assignable to type 'Bad'.
        Types of property 'ok' are incompatible.
          Type 'boolean' is not assignable to type 'false'.

Playground Link:

https://www.typescriptlang.org/play/index.html#src=interface%20Good%20%7B%0D%0A%20%20%20%20ok%3A%20true%2C%0D%0A%20%20%20%20data%3A%20string%2C%0D%0A%7D%0D%0A%0D%0Ainterface%20Bad%20%7B%0D%0A%20%20%20%20ok%3A%20false%2C%0D%0A%7D%0D%0A%0D%0Atype%20Result%20%3D%20Good%20%7C%20Bad%3B%0D%0A%0D%0Afunction%20convertPromiseDoesntCompile(p%3A%20Promise%3Cstring%3E)%3A%20Promise%3CResult%3E%20%7B%0D%0A%20%20%20%20return%20p.then(%0D%0A%20%20%20%20%20%20%20%20(data%3A%20string)%20%3D%3E%20(%7B%20ok%3A%20true%2C%20data%20%7D)%2C%0D%0A%20%20%20%20%20%20%20%20()%20%3D%3E%20(%7B%20ok%3A%20false%20%7D)%0D%0A%20%20%20%20)%3B%0D%0A%7D%0D%0A%0D%0Afunction%20convertPromiseCompiles(p%3A%20Promise%3Cstring%3E)%3A%20Promise%3CResult%3E%20%7B%0D%0A%20%20%20%20return%20p.then(%0D%0A%20%20%20%20%20%20%20%20(data%3A%20string)%20%3D%3E%20(%7B%20ok%3A%20true%20as%20true%2C%20data%20%7D)%2C%0D%0A%20%20%20%20%20%20%20%20()%20%3D%3E%20(%7B%20ok%3A%20false%20as%20false%7D)%0D%0A%20%20%20%20)%3B%0D%0A%7D

Related Issues:

#21427

@mhegazy
Copy link
Contributor

mhegazy commented May 25, 2018

Please see explanation in #19360

@mhegazy mhegazy added the Duplicate An existing issue was already created label May 25, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

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

3 participants