【问题标题】:Argument of type 'x' is not assignable to parameter of type 'x' in Angular7“x”类型的参数不能分配给 Angular7 中“x”类型的参数
【发布时间】:2019-07-22 16:07:53
【问题描述】:

我对 Angular 7 非常陌生。我在下面的代码行中遇到错误。我的代码有什么问题?

var x1 = 10, x2 = 100, x3 = datar.map(function (item) { return [ item.xmax ] })
   .reduce(function(a, b) { return Math.max(a, b); }); //error
var y1 = 10, y2 = 100, y3 = datar.map(function (item) { return [ item.ymax ] })
   .reduce(function(a, b) { return Math.max(a, b); }); //error

在上面的代码中我得到错误:number[] 类型的参数不能分配给number 类型的参数在'a,b'代码return Math.max(a, b)

var data = [ [0,0,0],[0,1,4],[0,2,1],[0,3,1]
                ,[1,0,0],[1,1,9],[1,2,4],[1,3,1]
                ,[2,0,0],[2,1,9],[2,2,9],[2,3,9]
                ,[3,0,0],[3,1,0],[3,2,0],[3,3,0] ];
     
data = data.map(function (item) { //error
    return [item[1], item[0], item[2] || '-'];
});

在上面的代码中我得到错误: Type (string | number)[][] is not assignable to type number[][]. 在“数据”

var objseries = datar.map(function(x) {
       return {
            type: 'scatter',
            symbol: 'diamond',
            symbolSize: 10,
            itemStyle:{
                normal:{ 
                    color:  x.color
                }
            },
            data: x.data.map(function (item) {
                return [
                item[0] <= x1 ? 0 + ((item[0] - 0) / ((x1 - 0) / 100)) / 100 :
                (item[0] <= x2 ? 1 + ((item[0] - x1) / ((x2 - x1) / 100)) / 100 :
                (item[0] <= x3 ? 2 + ((item[0] - x2) / ((x3 - x2) / 100)) / 100  : 2)),//error
               
                item[1] <= y1 ? 0 + ((item[1] - 0) / ((y1 - 0) / 100)) / 100 :
                (item[1] <= y2 ? 1 + ((item[1] - y1) / ((y2 - y1) / 100)) / 100 :
                (item[1] <= y3 ? 2 + ((item[1] - y2) / ((y3 - y2) / 100)) / 100  : 2)),//error
               
                item[0], item[1]
                ];
            }),
        }
    });

在上面的代码中我得到错误:

运算符'item[0] <= x3 ? 2 + ((item[0] - x2) / ((x3 - x2) / 100)) / 100 : 2))中'item[0]

和:

算术运算的左侧必须是代码“(x3-x2)”中的“any”、“number”、“bigint”或枚举类型at x3

已编辑

我在下面的代码中得到以下错误:

Type '(string | number)[][]' is not assignable to type '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'.ts(2322)
chart.component.ts(77, 13): The expected type comes from property 'data' which is declared here on type '{ type: string; symbol: string; symbolSize: number; itemStyle: { normal: { color: string; }; }; data: number[][]; }'

var datar = [
    {
        name: 'TEST-A',
        data: [
            [4,4],[25,4],[700,4],[4,40],[25,40],[160,40]
        ],
        xmax: 700,
        ymax: 40,
        color: 'purple',
    },
    {
        name: 'TEST-B',
        data: [
            [10.0, 8.04],
            [8.0, 6.95],
            [13.0, 7.58],
            [9.0, 8.81],
        xmax: 160,
        ymax: 400,
        color: 'green',
    }
];

var objseries = datar.map(function(x) {
   return {
        type: 'scatter',
        symbol: 'diamond',
        symbolSize: 10,
        itemStyle:{
            normal:{ 
                color:  x.color
            }
        },
        data: x.data.map(function (item) {
            return [
            item[0] <= x1 ? 0 + ((item[0] - 0) / ((x1 - 0) / 100)) / 100 :
            (item[0] <= x2 ? 1 + ((item[0] - x1) / ((x2 - x1) / 100)) / 100 :
            (item[0] <= x3 ? 2 + ((item[0] - x2) / ((x3 - x2) / 100)) / 100  : 2)),
           
            item[1] <= y1 ? 0 + ((item[1] - 0) / ((y1 - 0) / 100)) / 100 :
            (item[1] <= y2 ? 1 + ((item[1] - y1) / ((y2 - y1) / 100)) / 100 :
            (item[1] <= y3 ? 2 + ((item[1] - y2) / ((y3 - y2) / 100)) / 100  : 2)),
           
            item[0], item[1]
            ];
        }),
    }
}); 
    
objseries.splice(0, 0, {
    name: 'Severe Moderate Mild',
    xAxisIndex : 1,
    yAxisIndex : 1,
    type: 'heatmap',
    data: data, //error here
    label: {
        normal: {
            show: false
        }
    }
});

然后,此代码将进入 'series' 属性 https://echarts.apache.org/examples/en/editor.html?c=doc-example/scatter-visualMap-categories&edit=1&reset=1 (此链接仅用于说明关系)

我该如何解决这个问题?

【问题讨论】:

    标签: javascript angular typescript angular7


    【解决方案1】:

    这些错误都是打字稿错误。由于您没有任何明确的类型,也没有提到打字稿,我想知道您是否不知道正在使用打字稿。无论如何,这些错误的含义如下:

    'number[]' 类型的参数不可分配给代码'return Math.max(a, b)' 中'a, b' 处的'number' 类型参数

    这是指出 ab 是数组,而不是数字,因此对它们执行 Math.max 是行不通的。它们是数组的原因是因为您执行此操作的上一行:

    datar.map(function (item) { return [ item.ymax ] })
    

    您的意思可能是return item.ymax 而不是return [ item.ymax ]

    在上面的代码中我得到错误:类型'(string | number)[][]' is notassignable to type 'number[][]'。

    var data = 行中,您没有定义您的类型,因此打字稿做出了最好的猜测。由于您提供了一个每个元素都是数字的二维数组,因此打字稿推断data 的类型是number[][]。然后,当您尝试将字符串插入其中时,它指出字符串不是类型的一部分。如果您希望字符串成为一个选项,您需要自己指定类型,如下所示:

    var data: (string | number)[][] = [
      [0,0,0],[0,1,4],[0,2,1],[0,3,1],
      [1,0,0],[1,1,9],[1,2,4],[1,3,1],
      [2,0,0],[2,1,9],[2,2,9],[2,3,9],
      [3,0,0],[3,1,0],[3,2,0],[3,3,0]
    ];
    

    运算符'

    x3 是一个数组,因此尝试检查某个东西是否为&lt;= 是荒谬的。我的猜测是,当您解决第一个创建数组而不是单个数字的问题时,这个问题应该会消失。

    【讨论】:

    • 非常感谢@Nicholas 的帮助
    • 和以前一样,问题是你让打字稿猜测类型。你没有说这个 objseries 数组的类型是什么,所以它可以猜到的最好的结果是 data 属性的类型是number[][]。稍后,当您尝试拼接可能包含字符串的数据时,打字稿指出这不匹配。在定义 objseries 时添加显式类型,以便打字稿不需要猜测。
    • 感谢您的回复。我从 var 数据更改了代码: (string | number)[][] = [ [0,0,0],[0,1,4],[0,2,1],[0,3 ,1]....以此类推var数据:number[][] = [ [0,0,0],[0,1,4],[0,2,1] ,[0,3,1]...等等,但现在我在提到的(EDITED)部分遇到另一个错误,如下所示:
    • 新错误(继续上面的评论):类型参数'{名​​称:字符串; xAxisIndex:字符串; yAxisIndex:字符串;类型:字符串;数据:数字[][];标签:{正常:{显示:布尔值; }; }; }' 不能分配给 '{ type: string; 类型的参数;符号:字符串;符号大小:数字; itemStyle:{正常:{颜色:字符串; }; };数据:数字[][]; }'。对象字面量只能指定已知的属性,而 'name' 不存在于 type '{ type: string;符号:字符串;符号大小:数字; itemStyle:{正常:{颜色:字符串; }; };数据:数字[][]; }'
    • 只要打字稿强制猜测您的意思,这些问题就会不断出现。不可避免地,它有时会猜错。如果您想在项目中使用 typescript,那么我建议您仔细考虑您的类型并为它们编写显式接口。如果您希望这些对象有时有名称,有时没有,那么您可以在它们的界面中将 name 属性设为可选。
    猜你喜欢
    • 2019-01-27
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 1970-01-01
    • 2019-09-12
    • 2021-03-02
    相关资源
    最近更新 更多