最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Promise argument type is not assignable - Stack Overflow

matteradmin7PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far