Skip to content

TypeScript 1.5 - Metadata Annotations / Interfaces #2305

Closed

Description

@giancarloa

Hi there... I am struggling trying to find documentation on the TypeScript metadata annotation features that are apparently coming in 1.5... my question is how will interfaces work with metadata annotations knowing that TS interfaces have no runtime form???? take the following TS code:

class Server{
}

class Client{
constructor(svr: Server){
}
}

I have not been able to find much detail on how TS will handle this, but based on the old AtScript documentation, I believe the transpiled JS code will look something like this:

function Server(){
}

function Client(svr){
}

Client.parameters = [{is: Server}];

That's all fine and good but what will the JS look like if the TS is like that below whereby we introduce an interface??????

class IServer{
}

class Server implements IServer{
}

class Client{
constructor(svr: IServer){
}
}

Activity

  1. DanielRosenwasser commented on Mar 11, 2015

    @DanielRosenwasser
    Member

    See #2249 and the proposal for decorators in ES7.

    As for type serialization, interfaces will actually not carry any metadata (they will just represented as the Object constructor function); however, classes will be serialized. You can see the specifics for our the scheme here. Keep note that this is a work in progress and we're ironing out the details.

  2. giancarloa commented on Mar 12, 2015

    @giancarloa
    Author

    Thanks Daniel Rosenwasser (@DanielRosenwasser) for the info!!! I think I understand a little better now what's happening...

    it all looks great EXCEPT the interfaces... don't you think it's a serious problem that there's no way to query metadata for interfaces? In my case for example I would never ever do the first example I gave above. Instead I would always do the second which is program against an interface, and I would dare say this is usually the norm rather than the exception among devs. At the very least you guys could write out the fully qualified name of the interface. I never really understood why you guys don't produce a JS function for TS interface declarations.

    One other question I have... is TS going to always automatically generate basic metadata about TS classes OR do we always have to create and use decorators for this? For example, I believe AtScript was going to always generate metadata about the constructor function's parameter types without the developer having to do anything (see "Metadata Annotations" section of https://docs.google.com/document/d/11YUzC-1d0V1-Q3V0fQ7KSit97HnZoKVygDxpWzEYW0U/mobilebasic?viewopt=127). Is TS going to do the same? I hope so....

    Thanks again

  3. giancarloa commented on Mar 16, 2015

    @giancarloa
    Author

    since i got no response i guess it's best to close this item and ask my follow up questions in a new item.

  4. bparadie commented on May 7, 2015

    @bparadie

    giancarloa I found this article very helpful: https://smellegantcode.wordpress.com/2015/04/02/typescript-1-5-get-the-decorators-in/

    Here is the example in Playground:

    // https://smellegantcode.wordpress.com/2015/04/02/typescript-1-5-get-the-decorators-in/
    
    class C {
        @log
        foo(n: number) {
            return n * 2;
        }
    }
    
    function log(target: Function, key: string, value: any) {
        return {
            value: (...args: any[]) => {
    
                var a = args.map(a => JSON.stringify(a)).join();
                var result = value.value.apply(this, args);
                var r = JSON.stringify(result);
    
                console.log(`Call: ${key}(${a}) => ${r}`);
    
                return result;
            }
        };
    }
    
    var c = new C();
    console.log(c.foo(23));
    
  5. Ciantic commented on Jun 2, 2015

    @Ciantic

    Maybe this should be renamed as asking for documentation for annotations?

    I tried to find annotations from handbook, there is no mention of "@", "decorator" or "annotations". Also noticed the language spec is TS 1.4 so it wasn't helpful either.

  6. DanielRosenwasser commented on Jun 2, 2015

    @DanielRosenwasser
    Member

    Jari Pennanen (@Ciantic) I agree that we need to improve our documentation - the handbook is fairly outdated. We're currently fixing it up, and decorators are one of the new topics we intend to write about.

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

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions