【发布时间】:2019-06-30 18:56:42
【问题描述】:
我正在尝试定义一个可以包含数据或错误的对象。
export type ActionResult = {
data: any;
} | {
error: any;
};
function test():ActionResult {
return {
data: 3
}
}
当尝试访问我得到的函数结果时:
const v = test();
v.data = 23; // Property 'data' does not exist on type 'ActionResult'. Property 'data' does not exist on type '{ error: any; }'
访问“数据”或“错误”的正确方法是什么?
【问题讨论】:
标签: typescript