Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the @ (at sign) mean in the latest TypeScript (presumably v1.5) example?

Tags:

There is a very interesting picture was posted in the official TypeScript blog.

Wierd syntax

I wonder what the @ (at sign) symbol is doing there since (as far as I know) it cannot be used in JavaScript identifiers.

like image 463
Trident D'Gao Avatar asked Mar 06 '15 19:03

Trident D'Gao


People also ask

What is TypeScript example?

TypeScript is an open-source, object-oriented language maintained by Microsoft. Due to the static typing in TS, code written in TypeScript is more predictable and is generally easier to debug than normal JS. Before coding in TypeScript, we need to install it on our local machine.

What is any type and when to use it in TypeScript?

The any type allows us to assign literally “any” particular value to that variable, simulating what we know as plain JavaScript - where types can dynamically be assigned from different types, such as a String value becoming a Number.

What is TypeScript decorator?

A Decorator is a special kind of declaration that can be attached to a class declaration, method, accessor, property, or parameter. Decorators use the form @expression , where expression must evaluate to a function that will be called at runtime with information about the decorated declaration.


1 Answers

The big news this week is the merging of AtScript and TypeScript.

The following example from the AtScript documentation...

@Component() class MyApp {   server:Server;   @Bind('name') name:string;   @Event('foo') fooFn:Function;   @Inject()   constructor(@parent server:Server) {}   greet():string {} } 

Compiles into the following JavaScript...

function MyApp() {} MyApp.properties = {   'server': { is: Server },   'name': { is:string,             annotate: [new Bind('name']},   'fooFn': { is:Function,              annotate:[new Event('foo')]} } MyApp.annotate = [   new Component(),   new Inject() ]; MyApp.parameters = [   {is:Server, annotate:[parent]} ]; MyApp.prototype.greet = function() {} MyApp.prototype.greet.returns = string; 

AtScript was planned to be a layer on top of TypeScript (i.e. a super-set of a super-set) - but now the two projects are one.

Annotations are described thus:

  • AtScript annotation syntax is just a shorthand of placing the same information in ES5. It would be reasonable for an ES5 developer to write these annotations manually. A helper library could even be provided.
  • Annotations can only be placed on functions.

  • An annotation placed on a class is an annotation placed on the class’ constructor function.

  • An annotation placed on a field gets moved to the constructor function.

  • All annotations are translated as properties on a function.

like image 170
Fenton Avatar answered Oct 23 '22 15:10

Fenton