使用类型作为参数 Typescript [英] Using a type as a parameter Typescript

查看:52
本文介绍了使用类型作为参数 Typescript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Typescript 中使用类型作为参数

type myType = {}常量传递类型 = (t: 类型) =>{常量 x : t = {}}传递类型(我的类型);

我收到 TS 错误

't' 指的是一个值,但在这里被用作一种类型.您指的是 'typeof t' 吗?

'myType' 仅指一种类型,但在此处用作值.ts(2693)

打电话

关于如何实现这一点的任何想法.

解决方案

这是不可能的.


TypeScript 的静态类型系统(包括您的 myType 定义和类型注释)是 擦除 当代码作为 JavaScript 发出时.JavaScript 是在运行时实际运行的.在运行时,您只能访问.类型擦除意味着你上面的代码变成这样:

const passingType = (t) =>{常量 x = {};};传递类型(我的类型);

没有名为 myType 的值可以传递给 passingType.

而且由于 TypeScript 类型在运行时不作为值存在,因此不存在类型类型".就像你所说的 Type.因此,如上所述,这种方法不是直接可行的.


与其考虑传递类型"对于运行时的函数,这不是很有成效,最好在纯 JavaScript 中具体考虑您希望在运行时发生什么,然后编写类型来支持它.

你真的想用类型"做什么?在运行时?你想用它来检查一个值是否属于那种类型吗?然后,您可能希望传入 类型保护函数:

type Guard= (x: 任何) =>x是T;constpassingType = <T,>(t: Guard<T>) =>{如果(t(未定义)){console.log(未定义的受保护类型 T");}  别的 {console.log(undefined 不是受保护的类型 T");}}

你可以这样使用它:

function isUndefined(x: any): x is undefined {返回 typeof x === 未定义";}传递类型(isUndefined);//未定义的受保护类型 T 的 IS函数 isNumber(x: any): x 是数字 {return typeof x === "number";}传递类型(isNumber);//undefined 不是受保护的类型 Tfunction isNumberOrUndefined(x: any): x 是数字 |不明确的 {返回 isNumber(x) ||isUndefined(x);}传递类型(isNumberOrUndefined);//未定义的受保护类型 T 的 IS

您的实际用例将推动对 passingType 的参数应该是什么样子的要求.它可能是一个完整的数据结构,表示您想用类型"做的各种事情.在运行时.因此,即使这个类型保护示例对您不起作用,也有可能是其他东西.


但同样,简短的回答是 TypeScript 的静态类型系统已被删除,因此无法在运行时直接引用其类型.

游乐场连结代码

how to use a type as a parameter in Typescript

type myType = {}
const passingType = (t: Type) => {
    const x : t = {}
}

passingType(myType);

I get TS errors

't' refers to a value, but is being used as a type here. Did you mean 'typeof t'?

and

'myType' only refers to a type, but is being used as a value here.ts(2693)

for the call

any idea on how to accomplish this.

解决方案

This is not possible.


TypeScript's static type system (which includes your myType definition and type annotations) is erased when the code is emitted as JavaScript. JavaScript is what actually runs at runtime. At runtime, all you can access are values. Type erasure means your code above becomes something like this:

const passingType = (t) => {
    const x = {};
};
passingType(myType);

There's no value named myType to pass to passingType.

And because TypeScript types don't exist as values at runtime, there is no "type of types" like what you're calling Type. So this approach, as stated, is not directly possible.


Instead of thinking about passing a "type" to a function at runtime, which is not fruitful, it's probably best to think specifically about what you want to happen at runtime in pure JavaScript, and then write types to support that.

What would you really want to do with a "type" at runtime? Do you want to use it to check if a value is of that type? Then instead of passing in a type you might want to pass in a type guard function:

type Guard<T> = (x: any) => x is T;
const passingType = <T,>(t: Guard<T>) => {
    if (t(undefined)) {
        console.log("undefined IS of the guarded type T");
    }  else {
        console.log("undefined is NOT of the guarded type T");
    }
}

And you could use it like this:

function isUndefined(x: any): x is undefined {
    return typeof x === "undefined";
}

passingType(isUndefined); // undefined IS of the guarded type T

function isNumber(x: any): x is number {
    return typeof x === "number";
}
passingType(isNumber); // undefined IS NOT of the guarded type T

function isNumberOrUndefined(x: any): x is number | undefined {
    return isNumber(x) || isUndefined(x);
}

passingType(isNumberOrUndefined); // undefined IS of the guarded type T

Your actual use case will drive the requirements for what the argument to passingType should look like. It might be a whole data structure representing various things you'd want to do with a "type" at runtime. So even if this type guard example doesn't work for you, it's possible that something else will.


But again, the short answer is that TypeScript's static type system is erased and thus it is impossible to refer to its types directly at runtime.

Playground link to code

这篇关于使用类型作为参数 Typescript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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