Error Handling and Validation
Result-Style Unions
A result union returns either a success value or an error message without throwing.
result union
A result union uses a discriminant such as `ok` to separate success and failure paths.
Result-Style Unions
result.ts
Replay: real traced execution (multi-file project)
type ParseResult =
| { ok: true; value: number }
| { ok: false; error: string };
function parseCount(text: string): ParseResult {
const value: number = Number(text);
if (Number.isNaN(value)) {
return { ok: false, error: "not a number" };
}
return { ok: true, value: value };
}
const rawCount: string = "12";
const parsed: ParseResult = parseCount(rawCount);
if (parsed.ok) {
console.log(`count=${parsed.value}`);
} else {
console.log(`error=${parsed.error}`);
}
type ParseResult =
| { ok: true; value: number }
| { ok: false; error: string };
function parseCount(text: string): ParseResult {
const value: number = Number(text);
if (Number.isNaN(value)) {
return { ok: false, error: "not a number" };
}
return { ok: true, value: value };
}
const rawCount: string = "x";
const parsed: ParseResult = parseCount(rawCount);
if (parsed.ok) {
console.log(`count=${parsed.value}`);
} else {
console.log(`error=${parsed.error}`);
}
type ParseResult =
| { ok: true; value: number }
| { ok: false; error: string };
function parseCount(text: string): ParseResult {
const value: number = Number(text);
if (Number.isNaN(value)) {
return { ok: false, error: "not a number" };
}
return { ok: true, value: value };
}
const rawCount: string = "7";
const parsed: ParseResult = parseCount(rawCount);
if (parsed.ok) {
console.log(`count=${parsed.value}`);
} else {
console.log(`error=${parsed.error}`);
}
rawCount ← 12
10 return { ok: true, value: value };11}1213const rawCount→ 12: string = "12"; //@rawCount="x", "7"14const parsed: ParseResult = parseCount(rawCount12);value ← 12
2 | { ok: true; value: number }3 | { ok: false; error: string };45function parseCount(text12: string): ParseResult {6 const value→ 12: number = Number(text12);7 if (Number.isNaN(value)) {8 return { ok: false, error: "not a number" };9 }10 return { ok: true, value: value12 };11}parsed ← [object Object]
13const rawCount: string = "12"; //@rawCount="x", "7"14const parsed→ [object Object]: ParseResult = parseCount(rawCount12);if (parsed.ok)
13const rawCount: string = "12"; //@rawCount="x", "7"14const parsed: ParseResult = parseCount(rawCount);1516if (parsed.oktrue) {17 console.log(`count=${parsed.value12}`);18} else {outputcount=12
rawCount ← x
10 return { ok: true, value: value };11}1213const rawCount→ x: string = "x";14const parsed: ParseResult = parseCount(rawCountx);value ← NaN
2 | { ok: true; value: number }3 | { ok: false; error: string };45function parseCount(textx: string): ParseResult {6 const value→ NaN: number = Number(textx);7 if (Number.isNaN(value)) {if (Number.isNaN(value))
5function parseCount(text: string): ParseResult {6 const value: number = Number(text);7 if (Number.isNaN(valueNaN)) {8 return { ok: false, error: "not a number" };9 }parsed ← [object Object]
13const rawCount: string = "x";14const parsed→ [object Object]: ParseResult = parseCount(rawCountx);else
17 console.log(`count=${parsed.value}`);18} else {19 console.log(`error=${parsed.errornot a number}`);20}outputerror=not a number
rawCount ← 7
10 return { ok: true, value: value };11}1213const rawCount→ 7: string = "7";14const parsed: ParseResult = parseCount(rawCount7);value ← 7
2 | { ok: true; value: number }3 | { ok: false; error: string };45function parseCount(text7: string): ParseResult {6 const value→ 7: number = Number(text7);7 if (Number.isNaN(value)) {8 return { ok: false, error: "not a number" };9 }10 return { ok: true, value: value7 };11}parsed ← [object Object]
13const rawCount: string = "7";14const parsed→ [object Object]: ParseResult = parseCount(rawCount7);if (parsed.ok)
13const rawCount: string = "7";14const parsed: ParseResult = parseCount(rawCount);1516if (parsed.oktrue) {17 console.log(`count=${parsed.value7}`);18} else {outputcount=7