You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
exportenumServiceType{ServiceOperation='serviceOperation',BikeRent='bikeRent',OrderCharge='orderCharge',}exportinterfaceServiceAttributes{id: number;// sharedtype: ServiceType;// sharedduration?: number;// only ServiceOperationrentedBike?: string;// only BikeRentorderType?: string;// only OrderCharge}constbadService: ServiceAttributes={id: 2,type: ServiceType.ServiceOperation,}// is valid
Type '{ id: number; type: ServiceType.ServiceOperation; }' is not assignable to type 'ServiceAttributes'.
Type '{ id: number; type: ServiceType.ServiceOperation; }' is not assignable to type 'ServiceOperation'.
Property 'duration' is missing in type '{ id: number; type: ServiceType.ServiceOperation; }'.
😻 Type inference in control flow
letservice: ServiceAttributes;
Conditions (total)
if(service.type===ServiceType.ServiceOperation){service// let service: ServiceOperation}elseif(service.type===ServiceType.BikeRent){service// let service: BikeRent}else{service// let service: OrderCharge}
Conditions (partial)
if(service.type===ServiceType.ServiceOperation){service// let service: ServiceOperation}else{service// let service: BikeRent | OrderCharge}
Switch-case
switch(service.type){caseServiceType.ServiceOperation: {service// let service: ServiceOperationbreak;}default: {service// let service: BikeRent | OrderCharge}}
Non-imperative 😾
constisOperation=(service: ServiceAttributes)=>service.type===ServiceType.ServiceOperation;if(isOperation(service)){service// let service: ServiceAttributes}