admin管理员组

文章数量:1026989

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.

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.

本文标签: javascriptPromise argument type is not assignableStack Overflow