Type script
Why TypeScript
JavaScript is a powerful and widely used programming language, but it has a dynamic typing system, which means variable types are determined at runtime. While dynamic typing provides flexibility, it can lead to runtime errors that are challenging to catch during development.
What Typescript
In response to these challenges, Microsoft introduced TypeScript, a superset of JavaScript that adds static typing to the language. TypeScript is designed to address some of the limitations of JavaScript by providing developers with a more robust type system.
How Typescript
- Static Typing:
- TypeScript introduces static typing, allowing developers to declare the types of variables, parameters, and return values at compile-time.
- Static typing helps catch potential errors during development, offering a level of code safety that may not be achievable in pure JavaScript.
- But, it is there just while compilation. While runtime there is no type checking, it is still a plain JavaScirpt as the TypeScript transpiler converts it to JS. Even if a variable is of Number Type, it will happily accept a string on runtime.
- Compatibility with JavaScript:
- TypeScript is a superset of JavaScript, meaning that any valid JavaScript code is also valid TypeScript code.
- Developers can gradually adopt TypeScript in existing JavaScript projects without the need for a full rewrite.
- Tooling Support:
- TypeScript comes with a rich set of tools and features for development, including code editors (like Visual Studio Code) with built-in TypeScript support.
- The TypeScript compiler (tsc) translates TypeScript code into plain JavaScript, allowing it to run in any JavaScript environment.
- Enhanced IDE Experience:
- IDEs (Integrated Development Environments) that support TypeScript offer improved code navigation, autocompletion, and better refactoring capabilities.
- TypeScript's type information enhances the overall development experience.
- Interfaces and Type Declarations:
- TypeScript introduces concepts like interfaces and type declarations, enabling developers to define clear contracts for their code.
- Interfaces help document the shape of objects, making it easier to understand and maintain the code.
- Compilation:
- TypeScript code is transpiled to JavaScript during the compilation process, ensuring that the resulting code is compatible with various JavaScript environments and browsers.
Execution of TypeScript Code
TypeScript code doesn't run natively in browsers or JavaScript environments. Instead, it undergoes a compilation process to generate equivalent JavaScript code. Here's an overview of how TypeScript code is executed:
- Writing TypeScript Code:
- Developers write TypeScript code using
.ts or .tsx files, employing TypeScript's syntax with features like static typing, interfaces, and type annotations.
- TypeScript Compiler (tsc):
- The TypeScript Compiler (
tsc) is a command-line tool that processes TypeScript code.
- Developers run
tsc to initiate the compilation process.