在TypeScript中将类型返回为变量 [英] Returning a Type as a Variable in TypeScript

查看:240
本文介绍了在TypeScript中将类型返回为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在用TypeScript编写类工厂,并且想返回一个类型作为函数的输出.尽管TypeScript将类型作为输入进行处理-即泛型-美观地,我还没有找到一种将类型作为输出处理的方法.

I'm currently writing a class factory in TypeScript, and would like to return a type as the output of a function. Although TypeScript handles types as an input--i.e. generics--beautifully, I've not yet found a way to handle types as an output.

有关类工厂的StackOverflow问题一个特别有用的解决方案,但没有完全回答我的问题.鉴于以下结构,

This StackOverflow question on class factories offered a particularly helpful solution, but did not fully answer my question. Given the below structure,

function factory(someVar: any) {
    return class A {
        // Do something with someVar that makes this class unique
    }
}

这个问题表明,应该对该工厂函数的输出执行以下操作:

the question suggests that one should do the following with the output from this factory function:

import factory from "someLocation";

const AClass = factory({foo: "bar"});
type A = InstanceType<typeof AClass>;

interface IData {
    someField: A;
}

我有兴趣在工厂功能中包含此功能,以使系统更可重用或模块化.但是,如最初所述,我不确定如何从函数中返回类型.如果尝试以下操作,TypeScript会引发错误[ts] 'MenuState' only refers to a type, but is being used as a value here. [2693]:

I'd be interested in including this functionality in my factory function to make the system more reusable or modular. However, as initially stated, I'm unsure of how to return a type from a function. If I attempt the following, TypeScript throws the error, [ts] 'MenuState' only refers to a type, but is being used as a value here. [2693]:

function factory(someVar: any) {
    class AClass {
        // Do something with someVar that makes this class unique
    }

    type A = InstanceType<typeof AClass>;
    return A;
}

我该怎么办?通常,是否有任何语义上正确的方式将类型作为值或变量来处理?如果不是,为什么这违反了TypeScript中的最佳实践?此外,如果可能的话,该如何将这种类型用作构造函数?

How might I go about this? In general, is there any semantically correct way to handle types as values or variables? If not, why does this violate best practice in TypeScript? Furthermore, how might one then use this type as a constructor, if possible?

推荐答案

在语义上有没有正确的方法来将类型作为值或变量来处理

is there any semantically correct way to handle types as values or variables

不.类型仅在编译时.将类型表示为值,在运行时可用与语言设计非目标#5 在此列表中:

No. Types are compile-time only. Representing types as values, available at run-time is in the direct contradiction to the language design non-goal #5 in this list:

添加或依赖程序中的运行时类型信息,或根据类型系统的结果发出不同的代码.相反,应鼓励不需要运行时元数据的编程模式.

Add or rely on run-time type information in programs, or emit different code based on the results of the type system. Instead, encourage programming patterns that do not require run-time metadata.

唯一的例外是类,它们在运行时表示的方式与在es6 javascript运行时中表示的方式完全相同:作为构造函数.

The only exception are classes, which are represented at run-time exactly in the same way they are represented in es6 javascript runtime: as constructor function.

因此,您可以从函数中返回类,但是该函数将返回一个值,该值不能用作类型.唯一的例外(某种程度上)是,您可以使用函数调用表达式而不是使用类作为extends中的基类.

So you can return a class from a function, but the function will be returning a value, which can't be used as a type. The only exception (sort of) is that you can use a function-call expression instead of a class as a base class in extends.

另外,这个结构

InstanceType<typeof AClass>

编译器将

减少为AClass,如T的工具提示中所示%20AClass%20%7B%0D%0A%0D%0A%7D%0D%0A%0D%0Atype%20T%20%3D%20InstanceType%3Ctypeof%20AClass%3E%3B%0D%0A"rel =" nofollow noreferrer >此类型声明,其中表示type T = AClass

is reduced by the compiler to just AClass, as can be seen in a tooltip for T in this type declaration which says type T = AClass

type T = InstanceType<typeof AClass>;

这篇关于在TypeScript中将类型返回为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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