Search Terms
Suggestion
Right now, even with strictNullChecks is set to true, null cannot be used as a discriminator for tagged union.
Use Cases
Recently, I made a pull request for @types/node in which I let spawnSync returns a tagged union with error property as discriminator.
Examples
interfaceWithError{error: Errordata: null}interfaceWithoutError<Data>{error: nulldata: Data}typeDataCarrier<Data>=WithError|WithoutError<Data>// MAIN FOCUS:// TypeScript should be able to deduce error's and data's// types in the following function functiontest<Data>(carrier: DataCarrier<Data>){if(carrier.error===null){// `error` property as disciminatorconsterror: null=carrier.error// should workconstdata: Data=carrier.data// should work}else{consterror: Error=carrier.error// should workconstdata: null=carrier.data// should work}}Checklist
My suggestion meets these guidelines:
Search Terms
Suggestion
Right now, even with
strictNullChecksis set totrue,nullcannot be used as a discriminator for tagged union.Use Cases
Recently, I made a pull request for
@types/nodein which I letspawnSyncreturns a tagged union witherrorproperty as discriminator.Examples
Checklist
My suggestion meets these guidelines: