Aung Satt
Aung Satt

Reputation: 191

How TypeScript can defines Build-In Types to JavaScript on its compilation

I started studying on scripting languages especially JavaScript. It was fine and I started confused when I study Angular2 with TypeScript.

I realized that TypeScript can define Build-In Types such as string, number and Objects, and TypeScript will produce JavaScript on its compilation.

Here started my confusion, how TypeScript defines Generic Types on JavaScript since JavaScript is Prototype based Language and it can't define Types.

Example, how it is possible to compile the following codes into JavaScript.

interface Person {
   name : string,
   age : number
}

Can anyone explain this? Scripting is fun, but sometimes it is difficult to understand. (^^

Upvotes: 1

Views: 72

Answers (1)

Suren Srapyan
Suren Srapyan

Reputation: 68645

Typescript is a superset of JavaScript. It defines rules(generics, types and so on) only in Typescript level. It only defines generics in TypeScript level. So when you will see the Javascript-ed code, you can't see anything that is generic.

The same is with const. When you create a variable with const, you can't change it in TypeScript level. But you can change it in JavaScript, because

const myVarwill be parsed into var myVar, if it is not ES6

You can see here TypeScript to JavaScript, to which your codes is translating.

Upvotes: 3

Related Questions