在TypeScript/Angular中初始化类型化对象可以通过以下几种方式实现:
class Person {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
}
const person = new Person("John", 25);
const person = {
name: "John",
age: 25
};
const person = {} as Person;
person.name = "John";
person.age = 25;
const person = Object.assign({}, { name: "John", age: 25 });
以上是在TypeScript/Angular中初始化类型化对象的几种常见方式。根据具体的业务需求和代码结构,选择合适的方式来初始化对象。
领取专属 10元无门槛券
手把手带您无忧上云