Understanding Typescript

1) Definition:

Typescript is the Superset of Javascript.

TypeScript: A Superset of JavaScript

It's just compiles the compiles it to plain Javascript. It adds static types to javascript for better tooling and error handling.

Typescript is a development tool, your project still runs in js.

2) Difference from Javascript

a) Javascript is a dynamically types language, meaning types are determined at run-time.

b) Typescript add static typing, which means types are checked at compile-time.

Example:

// JavaScript (dynamic typing)
let message = "Hello, World!";
message = 123; // No error at compile-time

// TypeScript (static typing)
let message: string = "Hello, World!";
message = 123; // Error: Type 'number' is not assignable to type 'string'

3) Type Safety Feature in Typescript

Typescript type safety feature helps to catch errors early by enforcing type constraints.

Example:

function greet(name: string) {
  return "Hello, " + name;
}

greet("Alice"); // Correct
greet(42); // Error: Argument of type 'number' is not assignable to parameter of type 'string'

4) Static Checking

Typescript performs Static checking , it means helps to check the type error during the compile time rather than the runtime.

Example:

let check: boolean = "false";
check = "yes" // Error: Type "string" is not assignable to "boolean"

5. Types

TypeScript provides various types such as string, number, boolean, array, tuple, enum, any, and more.

Example:

let isCompleted: boolean = true; // boolean
let total: number = 100; // Number
let username: string = "John"; // String
let numbers: number[] = [1, 2, 3]; // array
let user: [number, string] = [1, "Alice"]; // Tuple

enum Direction {
  Up,
  Down,
  Left,
  Right
} // enum

let direction: Direction = Direction.Up;

6. Any

The any type allows opting out of type checking. It can hold any value.

Example:

let variable: any = "Hello";
variable = 42; // No error
variable = true; // No error
// In this any first assign type string to variable, then assign number to variable
// and finally assign boolean to variable.
// It changes on the value which is being hold.

7. noImplicitAny

The noImplicitAny flag in TypeScript’s configuration ensures that variables without explicit types are not automatically assigned the any type, which helps catch potential bugs.

Example:

function add(a, b) {
  return a + b;
}

// With noImplicitAny enabled, the above code will produce an error:
// Error: Parameter 'a' implicitly has an 'any' type.

8. Functions

TypeScript allows defining types for function parameters and return values, providing better type safety.

Example:

function multiply(a: number, b: number): number {
  return a * b;
}
// number which is assign out of the brackets, it is generally used for type of return value.
let result = multiply(2, 3); // Correct
result = multiply(2, "3"); // Error: Argument of type 'string' is not assignable to parameter of type 'number'

Conclusion

TypeScript enhances JavaScript by adding static types, which helps in catching errors early, improving code quality, and making development more predictable. By understanding its features and using them effectively, developers can build robust and maintainable applications.