Closed benjamin-hodgson closed 8 years ago
not fully fixed....
type Result<T> = { success: true, value: T } | { success: false };
function foo(): Result<number> {
if (someTest()) {
return { success: true, value: 42 }; //OK
return Object.assign({}, { success: true, value: 42 }) // type 'boolean' is not assignable to type 'true'
}
}
@bochen2014 I don't see how we could fix that case. In general we don't assume that functions won't modify a type within its own domain, and the true
value gets quickly widened to boolean
here because it's not used in a position where it's known to not be mutated.
hi Ryan, thanks for your reply. Just so you know that I've walked around the issue by
return {
success: true,
value :42,
...someObject
}
which does the same as Object.assign
TypeScript Version: nightly (2.1.0-dev.20160818)
Code I lifted this from Anders's final example in https://github.com/Microsoft/TypeScript/pull/9407#issue-162831239.
Expected behavior: Type-checks OK
Actual behavior: Type error:
If I add manual type annotations (
true as true
,false as false
) to thesuccess
assignments it works:The bug only affects Boolean literals. If I use enum, number, or string type literals it works - for example: