This repository contains my notes and examples from watching a TypeScript Tutorial Series on YouTube. Below are the key concepts and code snippets that illustrate the fundamentals of TypeScript as covered in the videos.
- Compile a TypeScript file to Javascript using:
tsc {typescript filename}
- To automatically compile files:
tsc {typescript filename} -w
- Declare types explicitly:
const variable_name: number = 1;
- Single type arrays:
let arr1: number[] = [1, 2, 3]; let arr2: string[] = ["one", "two", "three"];
- Mixed types arrays:
let arr3: (number|string)[] = [1, "two", 3];
- Declared objects with strict type checking:
let object: { name: string, age: number, beltColour: string, isPresent: boolean };
- Use union types to allow multiple types for variables:
let mixedArr: (string|number|boolean)[]; let uid: string|number;
- Use
anyfor a variable that can hold any type:
let dynamicVar: any;
- Initialize a TS project configuration file:
tsc --init
- Set directories for source and compiled output:
{
"rootDir": "./src",
"outDir": "./public",
},
"include": ["src"]
- Specify parameters and return types:
function add(num1: number, num2: number): number {
return num1 + num2;
}
- Create reusable type definitions:
type StringOrNum = string | number;
- Define the structure of a function:
let calculate: (x: number, y: number) => number;
calculate = (a, b) => a + b;
- Basic class structure:
class Invoice {
client: string;
details: string;
amount: number;
constructor(c: string, d: string, a: number) {
this.client = c;
this.details = d;
this.amount = a;
}
format() {
return `${this.client} owes $${this.amount} for ${this.details}`;
}
}
- Use
private,public, orreadonlyto control access to class members:
class Invoice {
constructor(
public client: string,
private details: string,
readonly amount: number
) {}
}
Importandexportclasses:
// Exporting
export class Invoice {}
// Importing
import { Invoice } from './classes/Invoice.js';
- Define a contract for
classesorobjects:
interface Person {
name: string;
age: number;
speak(words: string): void;
spend(amount: number): number;
}
- Use
genericsto createcomponentsthat work with various types:
function identity<T>(arg: T): T {
return arg;
}
interface GenericIdentityFn<T> {
(arg: T): T;
}
class GenericNumber<T> {
zeroValue: T;
add: (x: T, y: T) => T;
}
- Apply constraints to
generics:
interface Lengthwise {
length: number;
}
function loggingIdentity<T extends Lengthwise>(arg: T): T {
console.log(arg.length);
return arg;
}
Enumsorenumerationsallows for defining a set of named constants.
enum Direction { Up = 1, Down, Left, Right }
- Up is initialized with 1. All following members are auto-incremented from that point on. In this example, Down will be 2, Left will be 3, and so on.
enum Direction { Up = "UP", Down = "DOWN", Left = "LEFT", Right = "RIGHT" }
Stringenumsdo not auto-increment and each member must be initialized with a string value.let dir: Direction = Direction.Up;
Enumsare useful when you need a set of options whose values are known at compile time.
Tuplesallow you to express an array with a fixed number of elements whose types are known
let person: [string, number]; person = ["John", 35]; // Correct person = [35, "John"]; // Error: Type 'number' is not assignable >to type 'string'.
- Here,
personis a tuple where the first element is astringand the second is anumber.let address: [string, number, string?]; address = ["Park Lane", 123]; // OK address = ["Park Lane", 123, "Optional Additional Info"]; // Also >OK
Tuplescan have optional elements, denoted by the?. This makes the element at that position optional.let scores: [string, ...number[]]; scores = ["Math", 98, 97, 85]; scores = ["English"];
- This
tupleexpects astringas the first element, followed by anynumberof number types, denoted by...number[].