typeStatus="pending"|"active"|"archived";typeApiResponse=SuccessResult|ErrorResult;interfaceSuccessResult{
ok:true;
data: unknown;}interfaceErrorResult{
ok:false;
errorCode: number;
message: string;}functionhandleResponse(res:ApiResponse){if(res.ok){console.log(res.data);// narrowed to SuccessResult}else{console.error(res.message);// narrowed to ErrorResult}}
Output
// Union means 'one of these types' - must narrow before accessing specific members
Note You can only access properties common to ALL members of a union without narrowing first. Use a shared discriminant property (like 'ok' above) to let TypeScript narrow automatically in conditionals.
interfaceLoadingState{
status:"loading";}interfaceSuccessState{
status:"success";
data: string[];}interfaceErrorState{
status:"error";
errorMessage: string;}typeRequestState=LoadingState|SuccessState|ErrorState;functionrenderState(state:RequestState): string {switch(state.status){case"loading":return"Loading...";case"success":return`Got ${state.data.length} items`;// data is availablecase"error":return`Error: ${state.errorMessage}`;// errorMessage is available}}
Output
// Each case automatically narrows to the correct interface
Note A discriminated union has a common literal-typed property (the 'discriminant') shared across all members. TypeScript narrows exhaustively in switch/if on that property. This is the recommended pattern for modeling states, events, and message types.
Frequently asked questions
How does TypeScript handle discriminated union?
TypeScript covers this with 2 copy-ready snippets on this page. The "Union Types" snippet in TypeScript uses `type Name = TypeA | TypeB | TypeC;`.
Which code does the TypeScript example use?
The "Union Types" snippet uses `type Name = TypeA | TypeB | TypeC;`, from the Type Aliases section of the TypeScript cheat sheet.
What other TypeScript snippets are shown for "discriminated union"?
Besides "Union Types", this page also shows "Discriminated Unions".
Is there anything to watch out for?
Yes. For "Union Types": You can only access properties common to ALL members of a union without narrowing first. Use a shared discriminant property (like 'ok' above) to let TypeScript narrow automatically in conditionals.