Skip to content

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

Closed
@phorsuedzie

Description

@phorsuedzie

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions