Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument of type 'X' is not assignable to parameter of type 'X'

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

Getting following errors:

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

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

The Code:

DTO.ts

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

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:

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.

like image 538
Sasha Avatar asked Jan 12 '16 08:01

Sasha


People also ask

Is not assignable to parameter of type type?

The error "Argument of type string | undefined is not assignable to parameter of type string" occurs when a possibly undefined value is passed to a function that expects a string . To solve the error, use a type guard to verify the value is a string before passing it to the function.

How can I resolve the Argument type string can't be assigned to the parameter type int flutter?

to Solve The argument type 'String' can't be assigned to the parameter type 'int' Error Simple Solution is You have to set data the variable to List type.

How do I fix type string is not assignable to type never?

The error "Type is not assignable to type 'never'" occurs when we declare an empty array without explicitly typing it and attempt to mutate the array. To solve the error, explicitly type the empty array, e.g. const arr: string[] = []; .

Is not assignable to parameter of type type unknown?

The error "Argument of type 'unknown' is not assignable to parameter of type" occurs when we try to pass an argument of type unknown to a function that expects a different type. To solve the error, use a type assertion or a type guard when calling the function.


1 Answers

You miss parenthesis:

var value: string = dataObjects[i].getValue();  var id: number = dataObjects[i].getId(); 
like image 194
Martin Vseticka Avatar answered Oct 13 '22 01:10

Martin Vseticka