Dark mode
Complex Types (ประเภทเชิงซ้อน)
Object Types
Interface
ts
interface User {
id: number;
name: string;
email?: string; // Optional
}
const user: User = {
id: 1,
name: "John",
};
Type Aliases
ts
type Point = {
x: number;
y: number;
};
const center: Point = {
x: 0,
y: 0,
};
Function Types
ts
// Function Type Expression
type GreetFunction = (name: string) => string;
const greet: GreetFunction = (name) => `Hello, ${name}!`;
// Call Signatures
interface SearchFunc {
(source: string, subString: string): boolean;
}
Class Types
ts
class Person {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
greet(): string {
return `Hello, my name is ${this.name}`;
}
}
const person = new Person("John", 30);