In this case, you can use a switch
statement to narrow down which
type is represented at runtime:
ts
typeNetworkState =NetworkLoadingState |NetworkFailedState |NetworkSuccessState functionnetworkStatus (state :NetworkState ): string {// Right now TypeScript does not know which of the three// potential types state could be.// Trying to access a property which isn't shared// across all types will raise an errorProperty 'code' does not exist on type 'NetworkState'. Property 'code' does not exist on type 'NetworkLoadingState'.2339Property 'code' does not exist on type 'NetworkState'. Property 'code' does not exist on type 'NetworkLoadingState'.state .code // By switching on state, TypeScript can narrow the union// down in code flow analysisswitch (state .state ) {case 'loading':return 'Downloading...'case 'failed':// The type must be NetworkFailedState here,// so accessing the `code` field is safereturn `Error ${state .code } downloading`case 'success':return `Downloaded ${state .response .title } - ${state .response .summary }`}}
Intersection types are closely related to union types, but they are used very differently. An intersection type combines multiple types into one.