“X”类型的参数不能分配给“X”类型的参数

时间:2021-10-23 15:13:39

Good day. I'm new to Type Script, using VSCode.

美好的一天。我是使用VSCode的Type Script的新手。

Getting following errors:

获得以下错误:

  1. error TS2322: Type '() => string' is not assignable to type 'string'.

    错误TS2322:类型'()=>字符串'不能分配给'string'类型。

  2. error TS2322: Type '() => number' is not assignable to type 'number'.

    错误TS2322:类型'()=>数字'不能分配给'number'类型。

The Code:

代码:

DTO.ts

DTO.ts

interface DTO {

    getId(): number;
    getValue(): string;
}
export = DTO;

LinkedObject.ts

LinkedObject.ts

class LinkedObject {

    public value: string = "Not Set";
    public id: number = 0;

    constructor(value?: string, id?: number) {
        this.value = value;
        this.id = id;
    }
}
export = LinkedObject;

I am trying to instantiate LinkedObject class using above mentioned interface methods:

我试图使用上面提到的接口方法实例化LinkedObject类:

TravelClientFormPopulator.ts

TravelClientFormPopulator.ts

class TravelClientFormPopulator {

    public populateComboBoxUsingDTOs(dataObjects: Array<DTO>, comboBoxID: string): void {

        // Get the combo box
        var selectElement = <HTMLSelectElement> document.getElementById(comboBoxID);
        // Reset the options 
        selectElement.options.length = 0;

        var linkedObjectsToAdd: LinkedObject[] = new Array<LinkedObject>();

        var defaultLinkedObject: LinkedObject = new LinkedObject("Not Selected", 0);

        linkedObjectsToAdd.push(defaultLinkedObject);

        for (var i = 0; i < dataObjects.length; i++) {
            var value: string = dataObjects[i].getValue; // Error here
            var id: number = dataObjects[i].getId; // And here
            var linkedObject: LinkedObject = new LinkedObject(value, id);
        }
    }
}

Any help will be highly appreciated.

任何帮助将受到高度赞赏。

7 个解决方案

#1


33  

You miss parenthesis:

你错过了括号:

var value: string = dataObjects[i].getValue(); 
var id: number = dataObjects[i].getId();

#2


3  

Also adding other Scenarios where you may see these Errors

还添加其他方案,您可能会看到这些错误

  1. First Check you compiler version, Download latest Typescript compiler to support ES6 syntaxes

    首先检查编译器版本,下载最新的Typescript编译器以支持ES6语法

  2. typescript still produces output even with typing errors this doesn't actually block development,

    即使输入错误,typescript仍会生成输出,这实际上并不会阻止开发,

When you see these errors Check for Syntaxes in initialization or when Calling these methods or variables,
Check whether the parameters of the functions are of wrong data Type,you initialized as 'string' and assigning a 'boolean' or 'number'

当你看到这些错误在初始化中检查语法或者在调用这些方法或变量时,检查函数的参数是否是错误的数据Type,你初始化为'string'并指定'boolean'或'number'

For Example

例如

1.

1。

 private errors: string;
    //somewhere in code you assign a boolean value to (string)'errors'
    this.errors=true
    or 
    this.error=5

2.

2。

 private values: Array<number>;    
    this.values.push(value);  //Argument of type 'X' is not assignable to parameter of type 'X'

The Error message here is because the Square brackets for Array Initialization is missing, It works even without it, but VS Code red alerts.

这里的错误消息是因为缺少阵列初始化的方括号,即使没有它也可以工作,但VS Code红色警报。

private values: Array<number> = [];    
this.values.push(value);

Note:
Remember that Javascript typecasts according to the value assigned, So typescript notifies them but the code executes even with these errors highlighted in VS Code

注意:请记住Javascript根据指定的值进行类型转换,因此typescript会通知它们,但即使在VS Code中突出显示这些错误,代码也会执行

Ex:

例如:

 var a=2;
 typeof(a) // "number"
 var a='Ignatius';
 typeof(a) // "string"

#3


3  

For what it worth, if anyone has this problem only in VSCode, just restart VSCode and it should fix it. Sometimes, Intellisense seems to mess up with imports or types.

值得一提的是,如果任何人只在VSCode中遇到此问题,只需重启VSCode即可解决问题。有时,Intellisense似乎搞砸了进口或类型。

Related to Typescript: Argument of type 'RegExpMatchArray' is not assignable to parameter of type 'string'

与Typescript相关:类型'RegExpMatchArray'的参数不能分配给'string'类型的参数

#4


1  

I'm doing angular 2 and typescript and I didn't realize I had a space in my arrow notation

我正在做角度2和打字稿,我没有意识到我的箭头符号有空格

I had .map(key = > instead of .map(key =>

我有.map(key =>而不是.map(key =>

Definitely keep your eyes open for stupid syntax errors

绝对要留意愚蠢的语法错误

#5


0  

In my case, strangely enough, I was missing the import of the class it was complaining about and my IDE didn't detect it.

在我的情况下,奇怪的是,我错过了它抱怨的类的导入,我的IDE没有检测到它。

#6


0  

This problem basically comes when your compiler gets failed to understand the difference between cast operator of the type string to Number.

当您的编译器无法理解类型字符串的强制转换操作符与Number之间的区别时,这个问题基本上就出现了。

you can use the Number object and pass your value to get the appropriate results for it by using Number(<<<<...Variable_Name......>>>>)

您可以使用Number对象并传递您的值以使用Number(<<<< ... Variable_Name ...... >>>>)获取相应的结果

#7


0  

In my case, it was that I had a custom interface called Item, but I imported accidentally because of the auto-completion, the angular Item class. Be sure that you're importing from the right package.

在我的例子中,我有一个名为Item的自定义界面,但由于自动完成,我意外导入了角度Item类。确保您从正确的包装中导入。

#1


33  

You miss parenthesis:

你错过了括号:

var value: string = dataObjects[i].getValue(); 
var id: number = dataObjects[i].getId();

#2


3  

Also adding other Scenarios where you may see these Errors

还添加其他方案,您可能会看到这些错误

  1. First Check you compiler version, Download latest Typescript compiler to support ES6 syntaxes

    首先检查编译器版本,下载最新的Typescript编译器以支持ES6语法

  2. typescript still produces output even with typing errors this doesn't actually block development,

    即使输入错误,typescript仍会生成输出,这实际上并不会阻止开发,

When you see these errors Check for Syntaxes in initialization or when Calling these methods or variables,
Check whether the parameters of the functions are of wrong data Type,you initialized as 'string' and assigning a 'boolean' or 'number'

当你看到这些错误在初始化中检查语法或者在调用这些方法或变量时,检查函数的参数是否是错误的数据Type,你初始化为'string'并指定'boolean'或'number'

For Example

例如

1.

1。

 private errors: string;
    //somewhere in code you assign a boolean value to (string)'errors'
    this.errors=true
    or 
    this.error=5

2.

2。

 private values: Array<number>;    
    this.values.push(value);  //Argument of type 'X' is not assignable to parameter of type 'X'

The Error message here is because the Square brackets for Array Initialization is missing, It works even without it, but VS Code red alerts.

这里的错误消息是因为缺少阵列初始化的方括号,即使没有它也可以工作,但VS Code红色警报。

private values: Array<number> = [];    
this.values.push(value);

Note:
Remember that Javascript typecasts according to the value assigned, So typescript notifies them but the code executes even with these errors highlighted in VS Code

注意:请记住Javascript根据指定的值进行类型转换,因此typescript会通知它们,但即使在VS Code中突出显示这些错误,代码也会执行

Ex:

例如:

 var a=2;
 typeof(a) // "number"
 var a='Ignatius';
 typeof(a) // "string"

#3


3  

For what it worth, if anyone has this problem only in VSCode, just restart VSCode and it should fix it. Sometimes, Intellisense seems to mess up with imports or types.

值得一提的是,如果任何人只在VSCode中遇到此问题,只需重启VSCode即可解决问题。有时,Intellisense似乎搞砸了进口或类型。

Related to Typescript: Argument of type 'RegExpMatchArray' is not assignable to parameter of type 'string'

与Typescript相关:类型'RegExpMatchArray'的参数不能分配给'string'类型的参数

#4


1  

I'm doing angular 2 and typescript and I didn't realize I had a space in my arrow notation

我正在做角度2和打字稿,我没有意识到我的箭头符号有空格

I had .map(key = > instead of .map(key =>

我有.map(key =>而不是.map(key =>

Definitely keep your eyes open for stupid syntax errors

绝对要留意愚蠢的语法错误

#5


0  

In my case, strangely enough, I was missing the import of the class it was complaining about and my IDE didn't detect it.

在我的情况下,奇怪的是,我错过了它抱怨的类的导入,我的IDE没有检测到它。

#6


0  

This problem basically comes when your compiler gets failed to understand the difference between cast operator of the type string to Number.

当您的编译器无法理解类型字符串的强制转换操作符与Number之间的区别时,这个问题基本上就出现了。

you can use the Number object and pass your value to get the appropriate results for it by using Number(<<<<...Variable_Name......>>>>)

您可以使用Number对象并传递您的值以使用Number(<<<< ... Variable_Name ...... >>>>)获取相应的结果

#7


0  

In my case, it was that I had a custom interface called Item, but I imported accidentally because of the auto-completion, the angular Item class. Be sure that you're importing from the right package.

在我的例子中,我有一个名为Item的自定义界面,但由于自动完成,我意外导入了角度Item类。确保您从正确的包装中导入。