为什么object.constructor是“ Function”而不是“ newable”?在TypeScript中? [英] Why is object.constructor a "Function", and not "newable" in TypeScript?

查看:325
本文介绍了为什么object.constructor是“ Function”而不是“ newable”?在TypeScript中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在TypeScript中,我通常用以下类型定义类类型的类型:

In TypeScript, I usually define the type of a class type with:

declare type Type = { 
    new (...args: any[]): any
}

例如,当将类作为参数传递时,可以使用此方法。这有点类似于C#中的 Type ,但是也可以直接使用 new 实例化它。 。 AFAIK,此类型定义实际上类似于:

For example, this can used when a class is passed as an argument. This is kind-of similar to what Type is in C#, but it can also be instantiated directly with the new operator. AFAIK, this type definition is effectively analogous to:


一个构造函数,可以接收任意数量和类型的参数,并且可以返回任何值。

A constructor function that may receive any number and type of arguments, and may return anything.

如果期望参数,属性或其他变量包含对类的引用(更准确地说,是对其构造函数的引用) , Type 类型总是在手边,对我非常有用。

If an argument, property, or other variable is expected to contain a reference to a class (more precisely, to its constructor function), the Type type is always at hand, and is very useful to me.

但是,有时我遇到这样的情况:我没有对类(构造函数)本身的引用,仅对原型的引用。但是,仍然可以通过以下方式获得对类本身的引用:

However, sometimes I come across situations where I don't have a reference to the class (constructor function) itself, only its prototype. However, it's still possible to get a reference to the class itself by

obj.constructor

但是类型为 Function 的类型,并且无法实例化为类:

Which is of type Function however, and is not possible to instantiate as a class:

new obj.constructor(); // Error: Cannot use 'new' ...

这会产生错误:


不能将'new'用于其类型缺少调用或构造签名的表达式。

Cannot use 'new' with an expression whose type lacks a call or construct signature.

如果我使用类型断言,则编译器接受 new 运算符,并且编译后的代码在运行时将按预期方式运行。

If I use type assertions, the compiler accepts the new operator, and the compiled code behaves as expected at runtime.

是否缺少我要解释的东西,为什么 obj.constructor 不可现成?

Is there something I'm missing which explains why obj.constructor is not newable off-the-shelf?

推荐答案

因为TypeScript当前对构造函数属性没有特殊处理。在对象的lib.d.ts中定义。界面,因此只能将其一般定义为一个函数。

Because TypeScript currently has no special handling for the constructor property. It's defined in lib.d.ts for the Object interface, so it can only be generically defined as a Function.

https://github.com/Microsoft/TypeScript/issues/3841 https://github.com/Microsoft/TypeScript/issues/4356 是相关问题。

您可以执行以下操作:

You can do this:

class Foo {
    constructor(public x: string) { }
}
var foo = new Foo("foo");

var bar = new (foo.constructor as { new(x: string): typeof foo })("bar");

这使您可以对 foo (即,您不需要在类型断言中显式地编写 Foo ),但是您仍然需要显式地编写参数或使用<$ c $来完成c> ... args:任意[]

which allows you to be generic on the type of foo (i.e. you don't need to explicitly write Foo in the type assertion), but you still need to write the parameters explicitly or cop out with ...args: any[].

这篇关于为什么object.constructor是“ Function”而不是“ newable”?在TypeScript中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆