[英]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.