在Typescript中:是否有“编译时”方法来获取TypeScript接口中定义的所有属性名称? [英] In Typescript: Is there a 'compile-time' way to get all the property names defined in a TypeScript interface?
问题描述
我想创建一个通用的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
推荐答案
这是可能的使用 https://github.com/Microsoft/TypeScript/pull/13940,可在typescript @ next中找到。
This is possible by using custom transformers introduced by https://github.com/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接口中定义的所有属性名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!