代码之家  ›  专栏  ›  技术社区  ›  Ashish Patel

typescript ngrx类型在(string number)[]和string[]number[]之间不匹配

  •  0
  • Ashish Patel  · 技术社区  · 7 年前

    我正在尝试将我的Angular应用程序升级到5.2.10以及以下库: NGRX 5.2.0型 字体:2.6.2 TSLint 5.10.0标准

    在角度编译过程中碰到以下错误:

    `

    ERROR in src/app/**/xxx-state.ts(301,5): error TS2322: Type '(string | number)[]' is not assignable to type 'string[] | number[]'.
      Type '(string | number)[]' is not assignable to type 'number[]'.
        Type 'string | number' is not assignable to type 'number'.
          Type 'string' is not assignable to type 'number'.
    src/app/***/xxx-state.ts(305,5): error TS2322: Type '(string | number)[]' is not assignable to type 'string[] | number[]'.
      Type '(string | number)[]' is not assignable to type 'number[]'.
    

    `

    `

    error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '((searchElement: string, fromIndex?: number) => number) | ((searchElement: number, fromIndex?: nu...' has no compatible call signatures.
    

    `

    我正在使用@ngrx/entity library从ngrx的“module.d.ts”文件中定义的以下entity state接口派生我们的数据状态接口。

    `

    export interface EntityState<T> {
        ids: string[] | number[];
        entities: Dictionary<T>;
    }
    

    `

    有解决这些错误的指针吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Phil    7 年前

    (string|number)[] 可以包含字符串或数字元素的数组

    string[] | number[] 是字符串数组或数字数组。

    首先允许数组 [1, "2", 3] 第二个不允许。因此编译器会抱怨,为了您的安全。解决方案:只使用其中一个。