Good day. I'm new to Type Script, using VSCode.
Getting following errors:
error TS2322: Type '() => string' is not assignable to type 'string'.
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.
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.
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.
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[] = []; .
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.
You miss parenthesis:
var value: string = dataObjects[i].getValue(); var id: number = dataObjects[i].getId();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With