Typescript: Suggestion: reflection for interface?

Created on 23 Dec 2014  路  19Comments  路  Source: microsoft/TypeScript

Interface is a useful tool in TypeScript, but sometimes we'll need to write two pieces of almost identical code for intellisense and functionality respectively.

Is it acceptable to add a simple statement for generating part of interface information (properties)?

E.g.

class SomeClass { }

interface ISubFace {
    abc: string;
    def: number;
}

interface IFace {
    str: string;
    num: number;
    obj: SomeClass;
    sub: ISubFace;
}

var faceInfo = reflect IFace;

Would emit:

var SomeClass = (function () {
    function SomeClass() {
    }
    return SomeClass;
})();

var _ISubFaceReflection = {
    abc: 'string',
    def: 'number'
};
var _IFaceReflection = {
    str: 'string',
    num: 'number',
    obj: SomeClass,
    sub: _ISubFaceReflection
};

var faceInfo = _IFaceReflection;
Out of Scope Suggestion Too Complex

Most helpful comment

We don't need:

var obj: any;
var reflected = reflect obj;

but we do need:

interface A { /* ... */ }
var reflected = reflect A;

and this should be able to be done on compile-time.

All 19 comments

This will help my ObjectExtensions.ensure library a lot.

@SaschaNaz oops. sorry I din't read the comment carefully.

@vilic No, I didn't suggest anything but just said that I like your suggestion.

var face: IFace;
ObjectExtensions.ensure(face, reflect IFace);

This will be much better than:

var face: IFace;
ObjectExtensions.ensure(face, { str: "string", num: "number", obj: SomeClass, sub: { abc: "string", def: "number" } });

@SaschaNaz yeah, later I found that I misunderstood what you said... T-T my head is full of coffee, and it's too heavy to think...

@vilic Haha, me too. It's 1:44 AM here and I feel I have to sleep now. Well... Merry Christmas (or merry Christmas eve) :smile:

@SaschaNaz lol, Merry Christmas, and good night~ :D

Run time type information (ie reflection) is currently outside our design goals (https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals). If we were to do something like what you're requesting it would most likely surface in the form of attributes like #1557

Hmm, I thought it was easy because it seemed like we only need to do some sort of traversing... But I still look forward to it, annotations are annoying and it's not how JSers code. Any way we just need the certain reflection generated when reflect is there, and it should be somewhat compile-time...

We don't need:

var obj: any;
var reflected = reflect obj;

but we do need:

interface A { /* ... */ }
var reflected = reflect A;

and this should be able to be done on compile-time.

+1 for this feature. It will be of great help to deal with POJO ajax data and convert their types.

@meirgottlieb that is great but I guess it won't fit the needs. :'(

Will TypeScript have reverse-engineering features like Reflection in Java, PHP and C#? It would be great to when we say TypeScript will be used with server-side programming instead of Node.js etc. The annotation feature of TypeScript is great. Reflection would be greater!

:+1: I also think that it would be great to be able to access interfaces via reflection. Though I don't need that level of detail. The name of the interfaces would be enough.

https://github.com/Microsoft/TypeScript/issues/3148

+1, would be very useful for stubbing interfaces, a-la Mockito (http://mockito.org/), something like:

interface Animal {
   makeSound(): void;
}

const anAnimal = Mock.<Animal>create();
...
anAnimal.makeSound();

Unfortunately tscompiler-reflect project is discontinued. Is there any way to get runtime type check in TypeScript? RTTC was one of the main design goals of AtScript.

+1. I really hope to see typescript supports Java like reflection which is very useful for metadata driven designs such as use type information to generate auto-completion UI. The type information is valuable information which shouldn't be erased at runtime.

+1. This is a must have feature in any grown up language. One cannot be expected to rewrite all the types of an interface over again just to check the type.

The "keyof" and "reflect" is not the same! This has type information and can probably be usefull e.g in Angular2 Dependency Injection, injecting Interfaces.
We can have both or at least "reflect" that is more generic with.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rbuckton picture rbuckton  路  139Comments

Gaelan picture Gaelan  路  231Comments

nitzantomer picture nitzantomer  路  135Comments

metaweta picture metaweta  路  140Comments

kimamula picture kimamula  路  147Comments