TypeScript Version: 2.4.2
Code
Link to the playground
type One = {
kind: true;
foo: string;
}
type Two = {
kind: false;
foo: boolean;
}
type Options = One | Two;
const fn = (options: Options) => { /* ... */ };
const optionsOne = { kind: true, foo: 'bar' };
/**
* ERROR:
* Types of property 'kind' are incompatible.
* Type 'boolean' is not assignable to type 'false'.
*/
fn(optionsOne);
/**
* No Error.
*/
fn({ kind: true, foo: 'bar' })
Expected behavior: Should be possible to use true | false to create a discriminated union. It seems like true | false and boolean are not interchangeable.
Actual behavior: TypeScript does not widen the above kind to boolean and thus optionsOne can not be used as input for fn. (That's just my wild guess :-x) It's not possible to use true and false for discriminated unions.
TypeScript Version: 2.4.2
Code
Link to the playground
Expected behavior: Should be possible to use
true | falseto create a discriminated union. It seems liketrue | falseandbooleanare not interchangeable.Actual behavior: TypeScript does not widen the above
kindtobooleanand thusoptionsOnecan not be used as input forfn. (That's just my wild guess :-x) It's not possible to usetrueandfalsefor discriminated unions.