interfaces

This commit is contained in:
miqlangelo 2025-05-05 11:59:27 +02:00
parent fbbb0e7fcb
commit 5f87d616ee

View File

@ -0,0 +1,119 @@
interface IUser {
name: string;
email: string;
getInfo(): string;
}
type UserType = {
name: string;
email: string;
getInfo: () => string;
};
export class User implements IUser {
constructor(public name: string, public email: string) {}
getInfo(): string {
return `${this.name} (${this.email})`;
}
}
const _user: UserType = {
name: 'Bob',
email: 'bob@mail.com',
getInfo: function (): string {
return 'xy';
},
};
interface IAdmin extends IUser {
role: 'admin' | 'user';
}
const admin: Partial<IAdmin> = {
role: 'admin',
};
interface IUserExtended {
readonly id: string;
name: string;
email: string;
phone?: string;
}
const user: IUserExtended = {
id: 'id',
name: 'Fritz',
email: 'fritz@mail.com',
};
user.phone = '555';
user.name = 'Heinz';
// user.id = ""
interface UserAttribues {
propertyName: string;
[key: string]: string | number;
}
const userAttribues: UserAttribues = {
propertyName: 'value',
name: 'Charly',
};
console.log(userAttribues.propertyName);
console.log(userAttribues['propertyName']);
console.log(userAttribues['name']);
interface Animal {
name: string;
}
interface Animal {
age: number;
}
// const animal: Animal = {
// age
// }
interface Dog extends Animal {
breed: string;
}
type Vehicle = {
brand: string;
};
type Model = {
model: string;
};
// type Car = Vehicle & {
// model: string;
// }
type Car = Vehicle & Model;
const car: Car = {
model: 'X',
brand: 'Car',
};
console.log(car);
type Status = 'success' | 'error' | 'loading';
interface HttpResponse {
status: Status;
data: any;
}
interface HttpResponse {
headers: Record<string, string>;
}
const response: HttpResponse = {
status: 'success',
data: {},
headers: {},
};