TypeScript can describe not only variables, but also object structures, dictionary maps, and object-oriented models.
Object Types
const user: { name: string; age: number } = {
name: "Alice",
age: 25,
};Complex object shapes are usually extracted into type or interface.
interface User {
name: string;
age: number;
}Record Dictionaries
Record<K, V> describes key-value maps.
const userAges: Record<string, number> = {
Alice: 25,
Bob: 30,
};You can also restrict the allowed keys.
type UserName = "Alice" | "Bob" | "Carol";
const scores: Record<UserName, number> = {
Alice: 90,
Bob: 88,
Carol: 95,
};Index Signatures
interface ScoreMap {
[key: string]: number;
}Index signatures are useful for dynamic keys, but they allow any string key.
Classes
class Person {
constructor(
public name: string,
private age: number,
) {}
greet() {
return `Hello, ${this.name}`;
}
}Common access modifiers:
public: accessible outsideprivate: accessible only inside the classprotected: accessible inside the class and subclasses
Inheritance and Override
class Student extends Person {
override greet() {
return `Student: ${this.name}`;
}
}Abstract Classes
Abstract classes cannot be instantiated directly. They define requirements for subclasses and may include shared implementation.
abstract class Animal {
abstract speak(): string;
}
class Dog extends Animal {
speak() {
return "woof";
}
}Use abstract classes when you need both shared behavior and strong subclass contracts.