在Typescript中:是否有“编译时”方法来获取TypeScript接口中定义的所有属性名称?

编程入门 行业动态 更新时间:2024-10-24 07:23:46
本文介绍了在Typescript中:是否有“编译时”方法来获取TypeScript接口中定义的所有属性名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想创建一个通用的TypeScript类,用于渲染(作为HTML列表)一个实现特定TypeScript接口的对象数组。

I'd like to create a generic TypeScript class for rendering (as a HTML list) an array of objects which implement a specific TypeScript interface.

例如

class GenericListRenderer<T> { items: T[]; constructor(listItems: T[], className?: string){ this.items = listItems; ... } private getPropertyNames(): string[]{ // What is the best way to access all property names defined in // TypeScript interface 'T' that was used in this generic? ... } render(){ var propNames: string[] = this.getPropertyNames(); // render list with each item containing set of all // key(prop name)/value pairs defined by interface 'T' ... } }

问:什么是获得'编译时'的最佳方法'在指定的()TypeScript接口中定义的所有属性名列表?

Q: what would be the best way to get a 'compile-time' list of all property names defined in the specified () TypeScript interface?

与C ++模板一样,我相信TypeScript可以在编译时解析这些泛型,当TypeScript类型信息(如提供给用于实例化特定对象的通用的接口)很容易获得。

Like C++ templates, I believe that TypeScript could resolves these generics during "compile time", when TypeScript type information (like an interface supplied to the generic used to instantiate a particular object) is readily available.

由于可能提供了所有必需的类型信息,我只是好奇是否有一个TypeScript扩展/工具可用于访问此信息而不会过度运行时过滤' vanilla'Javascript对象 - 由于模糊的继承问题可能会出现问题(例如,如果运行时,泛型,Javascript(obj.hasOwnProperty(prop))用于过滤属性,则可能会过滤掉所需的TypeScript继承的接口属性。)

Since all the required type information is potentially supplied, I was just curious if there was a TypeScript extension/facility available to access this info w/o excessive runtime filtering of 'vanilla' Javascript objects -- which might be problematic due to ambiguous inheritance issues (e.g. desired TypeScript inherited interface properties may get filtered out if runtime, generic, Javascript (obj.hasOwnProperty(prop)) is used for filtering properties).

这个有用的属性内省潜力可以在编译时期间使用TypeScript的类型元数据超集来明确解析,而不是尝试在翻译的Javascript中解析此信息。这种类型的信息被丢弃了。

This useful property introspection potential could be unambiguously resolved with TypeScript's super-set of type meta-data during 'compile-time' vs trying to resolve this information in the translated Javascript, when all of this type information is discarded.

如果存在标准(TypeScript)方法,我讨厌'重新发明轮子'可能存在不完善的Javascript攻击。

I'd hate to 'reinvent-the wheel' with a potentially imperfect Javascript hack if a standard (TypeScript) approach exists.

此致:初学者TypeScript Guy, John

Sincerely: Beginner TypeScript Guy, John

推荐答案

这是可能的使用 github/Microsoft/TypeScript/pull/13940,可在typescript @ next中找到。

This is possible by using custom transformers introduced by github/Microsoft/TypeScript/pull/13940, which is available in typescript@next.

我的npm包, ts-transformer-keys 就是一个很好的例子。

My npm package, ts-transformer-keys, is a good example.

import { keys } from 'ts-transformer-keys'; interface Props { id: string; name: string; age: number; } const keysOfProps = keys<Props>(); console.log(keysOfProps); // ['id', 'name', 'age']

更多推荐

在Typescript中:是否有“编译时”方法来获取TypeScript接口中定义的所有属性名称?

本文发布于:2023-06-12 05:16:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/652028.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法来   属性   接口   定义   名称

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!