'compile-time' way to get all property names defined interface

后端 未结 4 1383
心在旅途
心在旅途 2020-12-03 05:00

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

e.g.

4条回答
  •  孤街浪徒
    2020-12-03 05:08

    This is not possible to retrieve that information at runtime, and they will never be per default possible unless you store them during the compilation. The solution is therefore be in reflection using ReflectDecorators.

    Here is an excellent article covering the question of retrieving compilation time metadata at the runtime. In short: you add a decorator to the interface you would like to keep the description, this one will be converted to a JSON object which one will be stored into the code itself. During the runtime, you will be able to retrieve this JSON object having all the interface data. This is now experimental (11th Feb 2016) but in a good way.

    Note: The reason why it will never by per default is basically a choice of design for TS not to overload the js code with metadata (unlike Dart).

提交回复
热议问题