简体   繁体   English

如何用构造函数/new关键字写typescript?

[英]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仍然保持car1any类型

How to make 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.

相关问题 Javascript-具有new关键字的函数构造函数 - Javascript - function constructor with new keyword 如何使用this关键字向构造函数添加新属性? - How to add a new property to constructor function using the this keyword? javascript / typescript 中的构造函数 function 和新 object - constructor function and new object in javascript / typescript 我如何在不使用构造函数(新)关键字的情况下调用 function 链接? - How can i call function chaining like without the use of the constructor(new) keyword? 如何为(构造函数,实例)方法编写typescript定义 - How to write typescript definition for is(constructor, instance) method Typescript中的工厂函数使用和不使用new关键字声明文件 - Factory function in a Typescript declares file, with and without the new keyword 如何在JavaScript中创建构造函数,以便如果使用'new'关键字创建的两个实例,instance1 === instance2的值返回true - How to create a constructor function in javascript so that if two instance created with 'new' keyword the value of instance1 === instance2 return true 如何将 js 函数构造函数转换为打字稿? - How to convert a js function constructor to typescript? TypeScript:如何生成构造函数? - TypeScript: how can generate constructor function? TypeScript:如何从javascript构造函数继承? - TypeScript: How to inherit from javascript constructor function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM