TypeScript Version: 2.0.2 RC
Code
exportinterfaceSome<T>{is_some: true;value: T;}exportinterfaceNone{is_some: false;}// fails typecheckvara: Option<number>={is_some: true,value: 1};Expected behavior:
Expected to pass typechecking.
Actual behavior:
The following error is raised:
src/option.ts(13,5): error TS2322: Type '{ is_some: boolean; value: number; }' is not assignable to type 'Option<number>'.
Type '{ is_some: boolean; value: number; }' is not assignable to type 'Some<number>'.
Types of property 'is_some' are incompatible.
Type 'boolean' is not assignable to type 'true'.
Adding a cast of the boolean value to type true appears to fix the error:
// working examplevara: Option<number>={is_some: <true>true,value: 1};I encountered this problem when experimenting with the following function. A version of the code which uses strings as the discriminator works without casts. Another version which uses value: null as the discriminator in the None case also works.
// failing exampleexportfunctionmap<T,N>(opt: Option<T>,fn: (value: T)=>N): Option<N>{if(opt.is_some){return{is_some: true,value: fn(opt.value)};}else{return{is_some: false};}}
TypeScript Version: 2.0.2 RC
Code
Expected behavior:
Expected to pass typechecking.
Actual behavior:
The following error is raised:
Adding a cast of the boolean value to type
trueappears to fix the error:I encountered this problem when experimenting with the following function. A version of the code which uses strings as the discriminator works without casts. Another version which uses
value: nullas the discriminator in theNonecase also works.