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);
Cardoesn't define index signature but function parameter has constraint for it. Could you explain what's the point of generics here? Bothfunction toStr(i: object) ...andfunction toStr(i: Record<string, any>) ...will worktoString(e.g.Object.create(null)), but typescript can't catch thiseslintcomplains when you use theobjecttype.