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:
Please see explanation in https://github.com/Microsoft/TypeScript/issues/19360
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.