我有我的主要文件中的以下代碼的命令行應用程序:從Typescript async await方法返回一個類型化的let值?
const init = async(): Promise<types.IContextObject> => {
let context: types.IContextObject;
try {
context = await createContext(dataObj);
} catch (e) {
dataObj.errors.insert({ message: e.message });
process.exit(1);
}
context.info.insert({
message: `Using Rules: `.bold + `${Object.keys(context.rules).join(', ')}`
});
return context;
};
在此我得到以下編譯器錯誤:
Variable 'context' is used before being assigned.
let context: types.IContextObject
如果我提出try塊內的最後兩行,我得到:
Function lacks ending return statement and return type does not include 'undefined'.