Reputation: 103
I have a class that has some public properties and I would like to pass it to a function with a generic argument of an index interface type, but I get an compiler error that says Argument of type 'Car' is not assignable to parameter of type '{ [k: string]: unknown; }'.
Could someone explain what is wrong because I assume all Classes are of type object?!!
class Car {
name: string | undefined;
}
function toStr <T extends { [k: string]: unknown }>(i: T) {
return i.toString();
}
const jeep = new Car();
jeep.name = 'jeep';
toStr(jeep);
Upvotes: 3
Views: 339
Reputation: 33051
Just use object
instead { [k: string]: unknown }
.
class Car {
name: string | undefined;
}
function toStr <T extends object>(i: T) {
return i.toString();
}
const jeep = new Car();
jeep.name = 'jeep';
const result = toStr(jeep);
Upvotes: 1