![]() |
TypeScript is a very mighty superset of JavaScript, which adds static typing to the language and allows developers to catch errors early on as well as write more maintainable code. This being said a TypeScript type system erases type information at compile time (or during the compilation), a phenomenon otherwise called “type erasure”. Understanding what happens at runtime with your code to type erasure is pivotal for TypeScript developers because this knowledge has far-reaching consequences both for architecture design decisions and application performance optimization strategies. High-Level LanguageJavascript is a high-level language. High-level languages are created for people and not machines, so they make it easier to read and write programs by abstracting away from the details of machine code and hardware interaction. For developers, they provide constructs and syntax that are similar to natural language. CompilingIn software development, compilation is the task of translating high-level language source code into machine code that can be run directly by a computer’s CPU. But in TypeScript, the word “compilation” is sometimes used in a less strict sense. The course of it, TypeScript compiler reads the code, looks for type errors and converts TypeScript code into JavaScript equivalent or similar to JavaScript so that browsers or other JavaScript environments can execute it. After the type checking is complete, the compiler does type erasure. That’s right, all of these types you’ve been writing in TypeScript, they’re gone. The reason for this is simple: JavaScript doesn’t know what to do with them. TranspilingTranspilation is a type of compilation that translates source-code written in one language into another equivalent source-code language, e.g. in TypeScript, transpiling means converting TypeScript code to JavaScript code. This way allows developers use some features of TypeScript like static typing when working with JavaScript environments. To be more specific, during transpilation (the process of converting TypeScript into JavaScript), any type annotations such as: string or interface definitions are taken out. Why? Because unlike TypeScript which has static typing and determines variable types before running a program, JavaScript does not care about such things until runtime. Type ErasureIn TypeScript, type erasure is when type annotations are stripped out while compiling so that only the JavaScript code remains for execution at runtime. Consequently, during compilation into JavaScript there’s no type information left because everything is in terms of plain old data types such as numbers or booleans etc there are no longer any references back to those static typing features from within our compiled output (which will be executed by machines). Example: To demonstrate the type erasure.
Output: Hello, WORLD Example: To demonstrate the compilation after above TypeScript code have been converted to the JavaScript.
Output: Hello, WORLD As you can see the type annotation: string is removed in the compiled JavaScript code. Consequences and Implications:
|
Reffered: https://www.geeksforgeeks.org
TypeScript |
Related |
---|
|
|
|
|
|
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |