This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface UserState { | |
| loading: boolean | |
| data?: User | |
| error?: Error | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Loading, but somehow also has data AND an error? | |
| const weird: UserState = { loading: true, data: user, error: new Error() } | |
| // Not loading, no data, no error — what does that even mean? | |
| const empty: UserState = { loading: false } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type UserState = | |
| | { status: 'loading' } | |
| | { status: 'success'; data: User } | |
| | { status: 'error'; error: Error } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function renderUser(state: UserState): string { | |
| switch (state.status) { | |
| case 'loading': | |
| return 'Loading…' | |
| case 'success': | |
| // TypeScript knows `state.data` exists here | |
| return `Welcome, ${state.data.name}` | |
| case 'error': | |
| // …and `state.error` exists here | |
| return `Something went wrong: ${state.error.message}` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type UserState = | |
| | { status: 'loading' } | |
| | { status: 'success'; data: User } | |
| | { status: 'error'; error: Error } | |
| | { status: 'refreshing'; data: User } // new |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function assertNever(value: never): never { | |
| throw new Error(`Unhandled state: ${JSON.stringify(value)}`) | |
| } | |
| function renderUser(state: UserState): string { | |
| switch (state.status) { | |
| case 'loading': | |
| return 'Loading…' | |
| case 'success': | |
| return `Welcome, ${state.data.name}` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type CheckoutState = | |
| | { step: 'cart'; items: CartItem[] } | |
| | { step: 'shipping'; items: CartItem[]; address: Address } | |
| | { step: 'payment'; items: CartItem[]; address: Address; method: PayMethod } | |
| | { step: 'confirmed'; orderId: string } | |
| type CheckoutAction = | |
| | { type: 'addAddress'; address: Address } | |
| | { type: 'addPayment'; method: PayMethod } | |
| | { type: 'confirm'; orderId: string } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Bad: `status` is `string`, so TypeScript can't narrow | |
| interface Loose { status: string; data?: User } | |
| // Good: literal union enables narrowing | |
| type Tight = { status: 'idle' } | { status: 'ready'; data: User } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface UserState { | |
| loading: boolean | |
| data?: User | |
| error?: Error | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Loading, but somehow also has data AND an error? | |
| const weird: UserState = { loading: true, data: user, error: new Error() } | |
| // Not loading, no data, no error — what does that even mean? | |
| const empty: UserState = { loading: false } |
NewerOlder