简体   繁体   English

TypeScript 中的构造函数,缺少什么?

[英]Constructor functions in TypeScript, what is missing?

I'm trying to figure out how to use type-safety provided by TypeScript with old plain constructor functions in JS.我试图弄清楚如何在 JS 中使用 TypeScript 提供的类型安全和旧的普通构造函数。 I have a very simple example, that looks straightforward, but I miss something and can't make it compile with TypeScript:我有一个非常简单的示例,看起来很简单,但我遗漏了一些东西并且无法使用 TypeScript 进行编译:

interface IMyService {
    new(): IMyService //I'm not sure if this line should be here, I just trying to make it working...
    doSomething(name: string): void
}

function MyService(this: IMyService): void {
    let _name = ""
    this.doSomething = (name) => {
        _name = name
    }
}

//The line below won't compile and it saying:
//"new" expression, whose target lacks a construct signature, implicitly has an "any" type
let service = new MyService();
service.setName("Test Name")  

What I'm missing?我缺少什么? I know the preferred way of using TypeScript is with "class", but in my case I would like to use simple constructor functions.我知道使用 TypeScript 的首选方式是使用“类”,但就我而言,我想使用简单的构造函数。

Why dont you want to use TypeScript's classes for this? 您为什么不想为此使用TypeScript的类? https://www.typescriptlang.org/docs/handbook/classes.html . https://www.typescriptlang.org/docs/handbook/classes.html

It will compile down to a function if your target is correct: TypeScript Playground 如果目标正确,它将编译为一个函数: TypeScript Playground

interface IMyService {
    doSomething(name: string): void
}

class MyService implements IMyService {
    constructor() {

    }
    doSomething(name: string) {

    }
}

You cant really type a function declaration (or at least i dont know how). 您不能真正键入函数声明(或者至少我不知道如何)。 However you can type a variable, and assign a function to it. 但是,您可以键入一个变量,并为其分配一个函数。 And then we can define a constructor type: 然后我们可以定义一个构造函数类型:

interface IMyService {    
  doSomething(name: string): void;
}

interface IMyServiceConstructor {
  new(): IMyService;
}

const MyService: IMyServiceConstructor = function(this: IMyService){
  //...
};

That can be shortified through using an inline type: 可以通过使用内联类型来简化:

const MyService: { new(): IMyService } = function(){
  //...
};

What's stopping you from doing this: 是什么阻止您执行此操作:

class MyService {
  // declare instance method
  doSomething: (x: string) => void;

  // this is really your function
  constructor() {
    let _name = "";
    this.doSomething = (name) => {
      _name = name;
    }  
  }
}
let service = new MyService();
service.doSomething("Test Name"); 

This emits almost the same code as your original. 这发出与原始代码几乎相同的代码。 It's still using a variable local to the constructor function scope, and an instance method instead of a class method. 它仍然使用构造函数范围内的局部变量,并使用实例方法代替类方法。 (Instance methods are generally frowned upon because you're creating closures for each instance but that's up to you.) (通常不赞成使用实例方法因为您要为每个实例创建闭包,但这取决于您。)

And TypeScript understands that MyService is newable and all the other goodness you want. 而且TypeScript知道MyService是可更新的,还有您想要的所有其他优点。 Jumping through hoops with constructor type signatures and convincing TypeScript that your function is the right type doesn't seem worth it to me. 在我看来,用构造函数类型签名跳过循环并说服TypeScript函数是正确的类型是不值得的。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase函数上的TypeScript中twilio.AccessToken是“不是构造函数” - twilio.AccessToken is “not a constructor” in TypeScript on Firebase Functions Javascript构造函数与Typescript类 - Javascript constructor functions vs Typescript classes Javascript-这些构造函数之间有什么区别? - Javascript - What is the difference between these constructor functions? 是什么( <typeof className> this.constructor)在打字稿中意味着什么? - what does (<typeof className>this.constructor) mean in typescript? TypeScript:在构造函数内部和外部声明变量有什么区别? - TypeScript: What is the difference between declaring variables inside the constructor and outside of it? Typescript 对象的构造函数中 Object.assign() 的目的是什么? - What is the purpose of Object.assign() in the constructor of a Typescript object? 在Javascript中,在什么条件下仅在构造函数中使用`this`是合适的? - In Javascript, under what conditions is it appropriate to only use `this` in constructor functions? 使用此JavaScript编码模式定义构造函数有什么好处? - What is the advantage of using this JavaScript coding pattern to define constructor functions? (function(global){}(this),构造函数和Javascript类之间有什么区别? - What is the difference between (function(global){}(this), constructor functions, and classes in Javascript? typescript 中的功能 - Functions in typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM