Repository navigation
Suggestion: reflection for interface? #1549
Description
Activity
saschanaz commented
on Dec 24, 2014 ContributorMore actionsThis will help my ObjectExtensions.ensure library a lot.
Reacted by Łukasz Kużyński, idchlife and François CabrolKagami Sascha Rosylight (@saschanaz) oops. sorry I din't read the comment carefully.
saschanaz commented
on Dec 24, 2014 ContributorMore actionsvilic 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" } });
Kagami Sascha Rosylight (@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...
saschanaz commented
on Dec 24, 2014 ContributorMore actionsvilic Haha, me too. It's 1:44 AM here and I feel I have to sleep now. Well... Merry Christmas (or merry Christmas eve) 😄
Kagami Sascha Rosylight (@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
- addedSuggestionAn idea for TypeScriptAn idea for TypeScriptToo ComplexAn issue which adding support for may be too complex for the value it addsAn issue which adding support for may be too complex for the value it addsOut of ScopeThis idea sits outside of the TypeScript language design constraintsThis idea sits outside of the TypeScript language design constraints
on Jan 6, 2015 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
reflectis there, and it should be somewhat compile-time...saschanaz commented
on Jan 6, 2015 ContributorMore actionsWe 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.
Reacted by Blue112, Alex Jeffrey and cerlestes+1 for this feature. It will be of great help to deal with POJO ajax data and convert their types.
Take a look at this project: https://github.com/artifacthealth/tsreflect-compiler.
Meir Gottlieb (@meirgottlieb) that is great but I guess it won't fit the needs. :'(
remojansen commented
on May 14, 2015 ContributorMore actions👍 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.
+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.- locked and limited conversation to collaborators
on Jun 18, 2018
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.
Would emit: