Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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'         ...      } } 

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

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.

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).

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.

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

like image 687
user3413621 Avatar asked May 13 '15 06:05

user3413621


People also ask

What is interface in tsx?

An interface contains the name of all the properties along with their types. It also includes the signature for functions along with the type of arguments and return type. For example, getTyrePressure and getRemCharging functions return the value of type number .

Why use interface TypeScript?

We should use Interface in TypeScript for validating the structure of an object in the case where we are creating an object, passing object as parameters. The interface is only available in TypeScript code when it compiles JavaScript code its existence disappears it doesn't make our final Source code heavier.

Why to use interface in Angular?

An interface is a way to define a contract on a function with respect to the arguments and their type. Along with functions, an interface can also be used with a Class as well to define custom types. An interface is an abstract type, it does not contain any code as a class does.


1 Answers

This is possible by using custom transformers introduced by https://github.com/Microsoft/TypeScript/pull/13940, which is available in typescript >= 2.4.1.

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'] 
like image 56
kimamula Avatar answered Oct 01 '22 15:10

kimamula