[英]Promise argument type is not assignable
I have the following method on an Angular component class:我在 Angular 组件类上有以下方法:
getPluginViaUrl(url: string): Promise<SCEPluginElement | string> {
const self = this;
return fetch(url, {cache: 'no-store'}).then(function (result) {
return result.text();
})
.then(function (code) {
const result = self.evalPluginCode(code);
if (!result.SCEPlugin) {
return Promise.reject('No SCEPlugin property exported from supposed plugin.');
}
if (!result.SCEPlugin.pluginName) {
return Promise.reject('SCEPlugin is missing a name (missing "pluginName" field)');
}
if (!result.SCEPlugin.pluginType) {
return Promise.reject('SCEPlugin is missing a type (missing "pluginType" field).');
}
return {
url: url,
code: code,
pluginName: result.SCEPlugin.pluginName,
pluginType: result.SCEPlugin.pluginType,
plugin: result.SCEPlugin
};
});
}
here is the type being used:这是正在使用的类型:
export interface SCEPluginElement {
url: string,
code: string,
pluginName: string,
pluginType: string,
plugin: Object
}
but I am getting this error:但我收到此错误:
ERROR in src/app/shared/services/utils-service.ts(57,13): error TS2345: Argument of type '(code: string) => Promise |
src/app/shared/services/utils-service.ts(57,13) 中的错误:错误 TS2345:'(code: string) 类型的参数 => Promise | { url: string;
{ 网址:字符串; code: string;
代码:字符串; pluginName: any;
插件名称:任意; pluginType: any;...' is not assignable to parameter of type '(value: string) => PromiseLike'.
pluginType: any;...' 不能分配给类型为 '(value: string) => PromiseLike' 的参数。 Type 'Promise |
输入'承诺| { url: string;
{ 网址:字符串; code: string;
代码:字符串; pluginName: any;
插件名称:任何; pluginType: any;
插件类型:任意; plugin: any;
插件:任何; }' is not assignable to type 'PromiseLike'.
}' 不可分配给类型 'PromiseLike'。 Type '{ url: string;
输入 '{ url: 字符串; code: string;
代码:字符串; pluginName: any;
插件名称:任意; pluginType: any;
插件类型:任意; plugin: any;
插件:任何; }' is not assignable to type 'PromiseLike'.
}' 不可分配给类型 'PromiseLike'。 Property 'then' is missing in type '{ url: string;
类型“{ url: string;”中缺少属性“then” code: string;
代码:字符串; pluginName: any;
插件名称:任意; pluginType: any;
插件类型:任意; plugin: any;
插件:任何; }'.
}'。
I cannot figure out what this error means.我无法弄清楚这个错误意味着什么。
You're receiving this error because you are returning Promise
s for your errors and not string
s.您收到此错误是因为您正在为错误返回
Promise
s 而不是string
s。
I'd suggest replacing all return new Promise
with throw new Error
.我建议用
throw new Error
替换所有return new Promise
。 Then you can use .catch
to handle the errors and simply return a SCEPluginElement
instead of SCEPluginElement | string
然后你可以使用
.catch
来处理错误并简单地返回一个SCEPluginElement
而不是SCEPluginElement | string
SCEPluginElement | string
. SCEPluginElement | string
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.