Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript 1.5 - Metadata Annotations / Interfaces #2305

Closed
giancarloa opened this issue Mar 11, 2015 · 6 comments
Closed

TypeScript 1.5 - Metadata Annotations / Interfaces #2305

giancarloa opened this issue Mar 11, 2015 · 6 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@giancarloa
Copy link

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){
}
}

@danquirk danquirk added the Question An issue which isn't directly actionable in code label Mar 11, 2015
@DanielRosenwasser
Copy link
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.

@giancarloa
Copy link
Author

Thanks @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

@giancarloa
Copy link
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.

@bparadie
Copy link

bparadie commented May 7, 2015

@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));

@Ciantic
Copy link

Ciantic commented Jun 2, 2015

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.

@DanielRosenwasser
Copy link
Member

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

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

5 participants