← Каталог
Асинхронное программирование в TypeScript — Состояние загрузки как discriminated union
Фрагмент из «Асинхронное программирование в TypeScript»: Состояние загрузки как discriminated union.
function renderUserState(state: UserLoadState): string {
switch (state.status) {
case "idle":
return 'Нажмите "Загрузить"';
case "loading":
return "Загрузка…";
case "success":
return state.data.name;
case "error":
return state.message;
default: {
const _exhaustive: never = state;
return _exhaustive;
}
}
} function renderUserState(state: UserLoadState): string {
switch (state.status) {
case "idle":
return 'Нажмите "Загрузить"';
case "loading":
return "Загрузка…";
case "success":
return state.data.name;
case "error":
return state.message;
default: {
const _exhaustive: never = state;
return _exhaustive;
}
}
}