Skip to content

Suggestion: reflection for interface? #1549

Description

@vilicvane

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;

Activity

  1. saschanaz commented on Dec 24, 2014

    @saschanaz
    Contributor

    This will help my ObjectExtensions.ensure library a lot.

  2. vilicvane commented on Dec 24, 2014

    @vilicvane
    Author

    Kagami Sascha Rosylight (@saschanaz) oops. sorry I din't read the comment carefully.

  3. saschanaz commented on Dec 24, 2014

    @saschanaz
    Contributor

    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" } });
  4. vilicvane commented on Dec 24, 2014

    @vilicvane
    Author

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

  5. saschanaz commented on Dec 24, 2014

    @saschanaz
    Contributor

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

  6. vilicvane commented on Dec 24, 2014

    @vilicvane
    Author

    Kagami Sascha Rosylight (@saschanaz) lol, Merry Christmas, and good night~ :D

  7. danquirk commented on Jan 6, 2015

    @danquirk
    Member

    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

  8. added
    SuggestionAn idea for TypeScript
    Too ComplexAn issue which adding support for may be too complex for the value it adds
    Out of ScopeThis idea sits outside of the TypeScript language design constraints
    on Jan 6, 2015
  9. vilicvane commented on Jan 6, 2015

    @vilicvane
    Author

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

  10. saschanaz commented on Jan 6, 2015

    @saschanaz
    Contributor

    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.

  11. ipy commented on Feb 1, 2015

    @ipy

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

  12. meirgottlieb commented on Feb 3, 2015

    @meirgottlieb
  13. vilicvane commented on Feb 4, 2015

    @vilicvane
    Author

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

  14. remojansen commented on May 14, 2015

    @remojansen
    Contributor

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

    #3148

  15. electricmonk commented on Dec 2, 2015

    @electricmonk

    +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();
  16. typeofweb commented on Feb 29, 2016

    @typeofweb

    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.

  17. jianwu commented on Mar 31, 2016

    @jianwu

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

  18. blue112 commented on Jul 21, 2016

    @blue112

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

  19. shumy commented on Feb 1, 2017

    @shumy

    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.

  20. locked and limited conversation to collaborators on Jun 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraintsSuggestionAn idea for TypeScriptToo ComplexAn issue which adding support for may be too complex for the value it adds

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions