Skip to content

Instantly share code, notes, and snippets.

@alex2534
Created March 15, 2025 15:25
Show Gist options
  • Save alex2534/4e9bf93202d24fe3c274003b19860979 to your computer and use it in GitHub Desktop.
Save alex2534/4e9bf93202d24fe3c274003b19860979 to your computer and use it in GitHub Desktop.
Modules
1. Basic Types
Overview:
TypeScript provides several basic types such as number, string, boolean, array, tuple, enum, any, void, null, undefined, never, and object.
Elements:
let isDone: boolean = false;
let age: number = 25;
let name: string = "Alex";
let numbers: number[] = [1, 2, 3];
let tuple: [string, number] = ["hello", 10];
enum Color { Red, Green, Blue }
let color: Color = Color.Green;
let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false;
function warnUser(): void {
console.log("This is my warning message");
}
let u: undefined = undefined;
let n: null = null;
Interfaces
Overview:
Interfaces define the structure of an object, specifying the types of its properties.
Elements:
interface Person {
firstName: string;
lastName: string;
age?: number; // optional property
}
function greet(person: Person) {
return `Hello, ${person.firstName} ${person.lastName}`;
}
let user = { firstName: "John", lastName: "Doe" };
console.log(greet(user));
Classes
Overview:
Classes in TypeScript provide a way to define objects and inheritance relationships.
Elements:
class Animal {
name: string;
constructor(theName: string) {
this.name = theName;
}
move(distanceInMeters: number = 0) {
console.log(`${this.name} moved ${distanceInMeters}m.`);
}
}
class Dog extends Animal {
bark() {
console.log("Woof! Woof!");
}
}
let dog = new Dog("Rex");
dog.bark();
dog.move(10);
Functions
Overview:
Functions in TypeScript can have typed parameters and return types.
Elements:
function add(x: number, y: number): number {
return x + y;
}
let myAdd = function(x: number, y: number): number { return x + y; };
let myAdd2: (x: number, y: number) => number =
function(x: number, y: number): number { return x + y; };
Generics
Overview:
Generics provide a way to create reusable components that work with multiple types.
Elements:
function identity<T>(arg: T): T {
return arg;
}
let output = identity<string>("myString");
function loggingIdentity<T>(arg: T[]): T[] {
console.log(arg.length);
return arg;
}
let output2 = loggingIdentity<number>([1, 2, 3]);
Type Aliases and Union Types
Overview:
Type aliases can create a new name for a type. Union types allow for variables to hold multiple types.
Elements:
type Name = string;
type NameResolver = () => string;
type NameOrResolver = Name | NameResolver;
function getName(n: NameOrResolver): Name {
if (typeof n === "string") {
return n;
} else {
return n();
}
}
type LinkedList<T> = T & { next: LinkedList<T> };
interface Person {
name: string;
}
let people: LinkedList<Person>;
people.name = "John";
people.next.name = "Doe";
Type Assertions
Overview:
Type assertions are a way to tell the compiler “trust me, I know what I’m doing.”
Elements:
let someValue: any = "this is a string";
let strLength: number = (someValue as string).length;
let strLength2: number = (<string>someValue).length;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment