跳到主要内容

Result<T, ErrorDataType>

type Result<T, ErrorDataType> = XOR<SuccessResult<T>, ErrorResult<ErrorDataType>>;

Defined in: type-definition/result-types.ts:36

结果返回类型,要么是成功结果,要么是错误结果,两者互斥 使用 XOR 类型确保结果对象要么有 data 属性,要么有 error 属性,不能同时存在

Type Parameters

Type ParameterDefault typeDescription
Tany成功数据类型,默认为 any
ErrorDataTypeany错误数据类型,默认为 any

Example

// 成功结果
const success: Result<string> = { data: '操作成功' };

// 错误结果
const error: Result<string, string> = { error: '操作失败' };

// 类型检查会阻止同时包含 data 和 error
// const invalid: Result = { data: 'ok', error: 'error' }; // 编译错误