javascript - Promise argument type is not assignable - Stack Overflow

I have the following method on an Angular ponent class:getPluginViaUrl(url: string): Promise<SCEPlug

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
Add a ment  | 

1 Answer 1

Reset to default 2

You're receiving this error because you are returning Promises for your errors and not strings.

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745657623a4638638.html

相关推荐

  • javascript - Promise argument type is not assignable - Stack Overflow

    I have the following method on an Angular ponent class:getPluginViaUrl(url: string): Promise<SCEPlug

    22天前
    70

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信