TypeScript Fundamentals

1. Thinking in Types

TypeScript encourages developers to think about data types explicitly, enhancing code clarity and catching potential errors early in the development process.

2. Components of TypeScript

TypeScript consists of three main components:

  1. Language: The TypeScript language itself, which is a superset of JavaScript.
  2. Compiler: Converts TypeScript code into JavaScript.
  3. Language Server: Provides intelligent code completion, refactoring, and other advanced features in compatible editors.

3. Compilation Output

When compiling a TypeScript file, two files are generated:

  1. JavaScript file: The runnable code.
  2. Declaration file (.d.ts): Contains all the type information.

This separation allows for type checking without affecting the runtime JavaScript code.

4. Type System Characteristics

5. Type Assertion

Type assertion allows you to tell the compiler to treat a value as a specific type:

let temp = 79 as any;

This forces TypeScript to view 79 as type any, leading to type inference of the temp variable as any.