A fully intensive TypeScript course for my programming colleagues at the company.
- 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.
Just follow these steps:
Make a directory and install typescript inside it.
mkdir tutorial
cd tutorial
npm i typescriptYou need a tsconfig.json file and this is how you get it.
npx tsc --initMake 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);Compile and run.
npx tscIt will generates your compiled TypeScript to JavaScript and you can run using node.
node dist/006-enums.js