admin管理员组文章数量:1435859
I have the following method on an Angular ponent class:
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 | { url: string; code: string; pluginName: any; pluginType: any;...' is not assignable to parameter of type '(value: string) => PromiseLike'. Type 'Promise | { url: string; code: string; pluginName: any; pluginType: any; plugin: any; }' is not assignable to type 'PromiseLike'. Type '{ url: string; code: string; pluginName: any; pluginType: any; plugin: any; }' is not assignable to type 'PromiseLike'. Property 'then' is missing in type '{ url: string; code: string; pluginName: any; pluginType: any; plugin: any; }'.
I cannot figure out what this error means.
I have the following method on an Angular ponent class:
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 | { url: string; code: string; pluginName: any; pluginType: any;...' is not assignable to parameter of type '(value: string) => PromiseLike'. Type 'Promise | { url: string; code: string; pluginName: any; pluginType: any; plugin: any; }' is not assignable to type 'PromiseLike'. Type '{ url: string; code: string; pluginName: any; pluginType: any; plugin: any; }' is not assignable to type 'PromiseLike'. Property 'then' is missing in type '{ url: string; code: string; pluginName: any; pluginType: any; plugin: any; }'.
I cannot figure out what this error means.
Share Improve this question asked Feb 17, 2018 at 20:05 Alexander MillsAlexander Mills 101k166 gold badges539 silver badges919 bronze badges 1- Your callback to then returns either a rejected promise or an object that isn't a promise, you need to be more consistent. I guess the typings can't quite handle the fact that returning the object is equivalent to returning a resolved promise of that object. – jonrsharpe Commented Feb 17, 2018 at 20:15
1 Answer
Reset to default 2You're receiving this error because you are returning Promise
s for your errors and not string
s.
I'd suggest replacing all return new Promise
with throw new Error
. Then you can use .catch
to handle the errors and simply return a SCEPluginElement
instead of SCEPluginElement | string
.
本文标签: javascriptPromise argument type is not assignableStack Overflow
版权声明:本文标题:javascript - Promise argument type is not assignable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745657622a2668803.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论