简体   繁体   English

Promise 参数类型不可分配

[英]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.

相关问题 'Promise 类型的参数<item | undefined> []' 不可分配给“SetStateAction”类型的参数<item[]> '</item[]></item> - Argument of type 'Promise<Item | undefined>[]' is not assignable to parameter of type 'SetStateAction<Item[]>' 打字稿:类型 &#39;Promise&lt;{}&gt;&#39; 不可分配给类型 - Typescript: Type 'Promise<{}>' is not assignable to type TypeScript:类型参数不可分配 - TypeScript: Argument of type is not assignable 类型参数不可分配给字符串 - Argument of type is not assignable to string 类型的参数不可分配给 - Argument of type not assignable to 打字稿:类型&#39;Promise &lt;{}&gt;&#39;不能分配给&#39;Promise&#39; <void> “ - Typescript : Type 'Promise<{}>' is not assignable to type 'Promise<void>' 打字稿错误:“数字”类型的参数不能分配给“预期”类型的参数 <Promise<number> &gt;” - Typescript Error: Argument of type 'number' is not assignable to parameter of type 'Expected<Promise<number>>' 参数类型不能分配给类型的参数 - Argument type is not assignable to parameter of type &#39;诺言 <void> &#39;不可分配给&#39;Promise <IListItem[]> - 'Promise<void>' is not assignable to type 'Promise<IListItem[]> [type]类型的参数不能分配给[type]类型的参数 - Argument of type [type] is not assignable to parameter of type [type]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM