Skip to content

BlackIQ/know-typescript-types

Repository files navigation

TypeScript tutorial

A fully intensive TypeScript course for my programming colleagues at the company.

Lessons

  • 001 Variables: Variables plus type annotation.
  • 002 Functions: Functions stricts in TypeScript.
  • 003 Interfaces: Make an object organized.
  • 004 Unions: One type of many types you say.
  • 005 Intersections: Combine 2 interfaces.
  • 006 Enums: Consume it is your dictionary.
  • 007 Generics: My favorite part of TypeScript.
  • 008 ReadOnly: Make a variable readonly.

How to get start?

Just follow these steps:

Step 1

Make a directory and install typescript inside it.

mkdir tutorial
cd tutorial
npm i typescript

Step 2

You need a tsconfig.json file and this is how you get it.

npx tsc --init

Step 3

Make a file and start writing code.

touch index.ts
// Union
type UUID = string | number;

// Enum
enum Department {
  Financial = "Financial",
  IT = "IT",
  ICT = "ICT",
  Software = "Software",
  HR = "HR",
}

// Interface
interface Employee {
  readonly id: UUID; // Using union
  department: Department; // Using enum
  readonly hireDate: Date; // This field is now read only!
  name: string;
  age: number;
}

// Make an employee
const Amirhossein: Employee = {
  id: 124578936,
  department: Department.ICT,
  hireDate: new Date(),
  name: "Amirhossein",
  age: 24,
};

console.log(Amirhossein);

Amirhossein.name = "Amirhossein Mohammadi";
// Amirhossein.id = 2; // You can't. ID is readonly
Amirhossein.department = Department.Software;

console.log(Amirhossein);

Step 4

Compile and run.

npx tsc

It will generates your compiled TypeScript to JavaScript and you can run using node.

node dist/006-enums.js

About

A practical, example-driven guide to TypeScript's type system.

Topics

Resources

License

Stars

3 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors