TypeScript Version: 3.0.0-dev.20180710
Search Terms: tagged discriminated union property this jsdoc checkjs allowjs literal
The following typescript works great:
typeDoneStatus={status: 'done';count: number;};typeErrorStatus={status: 'error';error: Error;};typeStatus=DoneStatus|ErrorStatus;classThing{s: Status;constructor(){this.s={status: 'done',count: 3};}fail(){this.s={status: 'error',error: newError()};}}However, the equivalent JS does not:
/** * @typedef DoneStatus * @property {'done'} status * @property {number} count *//** * @typedef ErrorStatus * @property {'error'} status * @property {Error} error *//** @typedef {DoneStatus | ErrorStatus} Status */classThing{constructor(){/** @type {Status} */this.s={status: 'done',count: 3};// ts error}fail(){this.s={status: 'error',error: newError()};// ts error}}Both assignments give the error:
[ts] Type 'string' is not assignable to type '"error" | "done"'.
test.js(3, 4): The expected type comes from property 'status' which is declared here on type 'DoneStatus | ErrorStatus'
(property) status: string
Expected behavior:
JS behaves the same as TS version
TypeScript Version: 3.0.0-dev.20180710
Search Terms: tagged discriminated union property this jsdoc checkjs allowjs literal
The following typescript works great:
However, the equivalent JS does not:
Both assignments give the error:
Expected behavior:
JS behaves the same as TS version