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

Created on 25 May 2018  路  2Comments  路  Source: microsoft/TypeScript

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

Duplicate

All 2 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dlaberge picture dlaberge  路  3Comments

CyrusNajmabadi picture CyrusNajmabadi  路  3Comments

Roam-Cooper picture Roam-Cooper  路  3Comments

blendsdk picture blendsdk  路  3Comments

MartynasZilinskas picture MartynasZilinskas  路  3Comments