Result type
A type, usually a discriminated union, that holds either a successful value or an error, so failures are part of the return type.
In more detail
type TaskResult = { ok: true; task: Task } | { ok: false; error: ProblemDetails } forces callers to check ok before using task. An Option type similarly holds either a value or nothing. Both are built with generic type aliases.
Related terms
- discriminated union — A type made of several object shapes that share one literal field, such as status or type, whose value tells them apart.
- type alias — A name given to any type with the type keyword, such as type HttpMethod = "GET" | "POST".
- generics — Types that take other types as parameters, so one definition works for many kinds of value.
Taught in
- The Compiler Pipeline: tsc, AST And Code Emission — Production TypeScript
- tsconfig.json: target, lib, strict And NodeNext — Production TypeScript
- any Versus unknown: Defensive Typing In Practice — Production TypeScript
- void, undefined And null: Exact Optional Property Types — Production TypeScript
- never: The Bottom Type And Exhaustive Switch Checking — Production TypeScript
- Union Types, Intersections And Property Conflicts — Production TypeScript
- Discriminated Unions: Tagged Variants And Pattern Matching — Production TypeScript
- Generic Interfaces, Classes And Type Aliases — Production TypeScript
- Default Generic Arguments And Multi-Type Parameters — Production TypeScript
- Core Utilities: Partial, Required, Readonly, Pick And Omit — Production TypeScript
- Function Utilities: ReturnType, Parameters And Constructor — Production TypeScript
- Conditional Types: Ternary Types And Type Inference With infer — Production TypeScript
- ECMAScript Modules, CJS Interop And verbatimModuleSyntax — Production TypeScript
- The Spine Artifact: Generating Typed Models From OpenAPI — Production TypeScript