1
如果我有一个返回值或者从数据库或信息的空对象这样的对象的功能:流程,对象与工会文字不兼容
getThingFromDB: async function(id:string):Promise<EventObj|Empty>{
const fromDB = await dao.getEvent(id);
if(fromDB && fromDB.length){
return fromDB[0];
}
return {};
}
我得到了很多像流的错误:
return {};
^^ object literal. This type is incompatible with
getThingFromDB: async function(id:string):Promise<EventObj|Empty>{
^^^^^^^^^^^^^^ union: EventObj | Empty
或者
getThingFromDB: async function(id:string):Promise<EventObj|Empty>
^^^^^^^^ property `id`. Property not found in
return {};
^^ object literal
这里是我的声明的对象类型。
declare type EventObj = {
id:string,
name:string,
urlName:string
};
declare type Empty = {||};
我在想什么?我如何正确地解决这些错误。
尝试在函数体内显式注释。我想流具有麻烦推断类型的返回值的由于条件分支(尝试删除分支或冷冻/密封对象常量'返回Object.freeze({});') – ftor
'Object.freeze({ });'做了诡计,谢谢!如果你改变这个答案,我会接受它作为正确的答案。 – Newtang