[英]How to write typescript with constructor function/new keyword?
This is an example from MDN docs for the usage of new
keyword这是MDN 文档中使用
new
关键字的示例
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:我相信,function 的 TS 版本是:
/* 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.这在编写方法时为我提供了智能感知/自动完成功能,但我如何在使用
new
关键字创建实例时使 TS 推断类型。 Even after typescripting the method, creating an instance like this:即使在对方法进行打字之后,创建一个这样的实例:
const car1 = new Car('Eagle', 'Talon TSi', 1993);
still keeps car1
as type any
仍然保持
car1
为any
类型
car1
's type infer to be Car
method's this
type?car1
的类型推断为Car
方法的this
类型? (without explicitly setting type for the instance object, const car1: CarDetails = new Car(...;
) (没有为实例 object 显式设置类型,const
car1: CarDetails = new Car(...;
)
Obviously the class syntax should be the way to go, but as you I'm also running in circles trying to find the right solution that outputs the old valid JS syntax.显然 class 语法应该是通往 go 的方式,但正如你一样,我也在兜圈子,试图找到输出旧的有效 JS 语法的正确解决方案。 This is a good approximation that I've found in this random answer and it's poorly explained in the official Typescript documentation .
这是我在这个随机答案中找到的一个很好的近似值,但在Typescript 官方文档中对此解释得很差。 Here is the relevant part that I've modified to make it better:
这是我修改以使其更好的相关部分:
// Only instance members here.
export interface Circle { // Name the interface with the same than the var.
radius: number;
area: () => number;
perimeter: () => number;
}
// You could define static members here.
interface CircleConstructor {
new(radius: number): Circle;
}
export const Circle = function(this: Circle, radius: number) {
const pi = 3.14;
this.radius = radius;
this.area = function () {
return pi * radius * radius
}
this.perimeter = function () {
return 2 * pi * radius;
}
} as unknown /*as any*/ as CircleConstructor; // Note the trust-me casting
const c = new Circle(3); // okay
This comes with some issues.这带来了一些问题。 For instance, the fact that it's using
any
type which is forbidden or the necessary use of the as
operator which is mega-ugly.例如,它正在使用
any
被禁止的类型,或者必须使用as
操作符,这是非常丑陋的。 As improvement, I've used unknown
instead of any
but the core problem remains.作为改进,我使用了
unknown
而不是any
但核心问题仍然存在。 This makes the lint tool to not complain, so it's a big improvement over the any
.这使得 lint 工具不会报错,所以它比
any
有了很大的改进。
This is the best solution I've found so far.这是迄今为止我找到的最佳解决方案。 Typescript devs are aware of this, but they have decided to not provide support for this kind of "syntax".
Typescript 开发人员意识到了这一点,但他们决定不为这种“语法”提供支持。
You could use classes instead:您可以改用类:
class Car { make: string; model: string; year: number; constructor(make: string, model: string, year: number) { this.make = make; this.model = model; this.year = year; } } // usage is the same const car1 = new Car('Eagle', 'Talon TSi', 1993);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.