This is my journey to learn TypeScript part 1.

I decided to learn TypeScript to make my code more stable and to add static typing to JavaScript. Static typing means that the type of a variable cannot be changed at any point in a program.

Why TypeScript?

Fundamentals

Basic Types

Function

How to declare basic types in functions.

function sum(a: number, b: number): number {
  return a + b;
}

Union Type

// Using union type function getAge(age: number | string) { if (typeof age === ‘number’) { age.toFixed(); return age; } if (typeof age === ‘string’) { return age; } return new TypeError(‘age must be number or string’); }

- **Intersection Type**
	- An intersection type is a type that merges several kinds into one.

interface Person { name: string; age: number; } interface Developer { name: string; skill: number; } type Capt = Person & Developer;


### Type Aliases

- Type aliases create a new name for a type. Type aliases are sometimes similar to interfaces, but can name primitives, unions, tuples, and any other types that you’d otherwise have to write by hand.

// Using string type const name: string = ‘brian’;

// Using type aliases type MyName = string; const name: MyName = ‘brian’;

type Developer = { name: string; skill: string; }

type User = { name: T } ```

Other tips

  1. How to compile ts file to js
    • npm i typescript -g
    • tsc filename.ts
    • create tsconfig.json to set configuration details
      • https://www.typescriptlang.org/tsconfig