type Action<T extends string, P = void> = P extends void↵
? { type: T }↵
: { type: T; payload: P };↵
type LoadingAction = Action<"LOADING">;↵
type SuccessAction = Action<"SUCCESS", { data: string[] }>;↵
type ErrorAction = Action<"ERROR", { message: string }>;↵
type AppAction = LoadingAction | SuccessAction | ErrorAction;↵
function reducer(action: AppAction): string {↵
switch (action.type) {↵
case "LOADING": return "Loading...";↵
case "SUCCESS": return action.payload.data.join(", ");↵
case "ERROR": return action.payload.message;↵
}↵
}↵
console.log(reducer({ type: "SUCCESS", payload: { data: ["a", "b"] } }));