What is Type Annotation?

Asked

Viewed 177 times

10

I’m reading a book about Typescript and I came across something that caught my attention which is the Type Annotation.

Take the example:

function foo(): { a: number, b?: number } {
    if (this.a <= 0) { 
        return { a: 1 };
    }
    else {
        return { a: 2, b: 4 };
    }
}

I found this syntax a little different than I’m used to, and I got confused and with some doubts about this feature.

Doubts

  1. What is Type Annotation?
  2. What scenarios does Type Annotation apply to?
  3. Type Annotation is restricted to functions only?
  4. Type Annotation would be the same as arguments of a function?

1 answer

8


What is Type Annotation?

Technically it is every syntax that a given value or variable or other location where there may be a value has its type explicitly defined. The term is mostly used in languages, such as Typescript, which force the type in the compilation as if it were static typing, but it will actually run dynamically, since in Javascript its backend basic has dynamic typing. The term applies to other languages but as usual is to have the annotation of type the term is not so used.

What scenarios does Type Annotation apply to?

Everyone who wants type security and needs to say this explicitly. So it occurs in all static typing languages or wishing to simulate characteristics of this typing model.

Type Annotation is restricted to functions only?

No, there’s just a way to use type annotation, actually, every note you’ve ever seen on TS or other languages is a type Annotation, only had not seen the term before precisely because it is not so common, you use medium without realizing.

Type Annotation would be the same as arguments of a function?

I do not know if deep down you are worried about the return of an object, this is something that JS already accepts, you are creating an anonymous object with members a and b and it’s coming back to this. What’s different about Typescript is that you can tell what the type of this object is in the function return, so you can tell which are the members, their names and their types. This gives type security because it cannot change and return something different without error, or it cannot use this wrong return in a location that expects exactly this type.

Note that this is an anonymous type, it has a structure but not a name, so instead of the name you see the structure of this type, but it is still a type. Read more about these definitions in What is typing style?.

Another thing that may seem strange is that you may be used to syntax in languages where the function return type comes before the name. The more modern languages are adopting the type later, it is more correct and easier to parse.

So there are two concepts there: one is the return of an object and the other is the typing of it. Note that there in the book has the example as it is in JS, the only difference is the typing, therefore the presence of the type annotation.

This is also a type note:

function foo() : number {
    return 1;
}

I put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.