This is an example from MDN docs for the usage of new keyword
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
const car1 = new Car('Eagle', 'Talon TSi', 1993);
I believe, the TS version of the function would be:
/* car type */
type CarDetails = {
make: string;
model: string;
year: number;
}
/* setting this to type CarDetails */
function Car(this:CarDetails, make: string, model:string, year:number) {
this.make = make;
this.model = model;
this.year = year;
}
This gives me intellisense/autocomplete while writing the method but how do I make TS infer types while creating an instance with new keyword. Even after typescripting the method, creating an instance like this:
const car1 = new Car('Eagle', 'Talon TSi', 1993);
still keeps car1 as type any
How to make car1's type infer to be Car method's this type ?
(without explicitly setting type for the instance object, const car1: CarDetails = new Car(...; )
classhere?