当我试图解构以下函数的返回类型时:
const coreml = async (
pathToImage: string,
): Promise<{label: string; confidence: string} | undefined> => {
//body
};因此:
const {label, confidence} = await coreml(/*path to image*/);我得到了
'confidence' is assigned a value but never used.eslint@typescript-eslint/no-unused-vars
Property 'confidence' does not exist on type '{ label: string; confidence: string; } | undefined'.发布于 2021-07-20 03:45:29
函数的结果是{label: string, confidence: string}或undefined。哪一个在编译时是未知的。但是您不能将undefined解构为label和confidence,而typescript希望确保类型安全。这样就产生了错误。
原则上,解构的工作方式如下:
const temp = await coreml();
//temp is now either {"label": "foo", "confidence": "bar"} or undefined
//but the next statements will throw an error, if temp is undefined
const label = temp.label;
const confidence = temp.confidence;Typescript希望确保,此错误不会在运行时发生。因此,编译时的错误
https://stackoverflow.com/questions/68446076
复制相似问题