Typescript 将对象映射到类属性,如接口
我喜欢在Typescript界面中执行以下操作:Typescript 将对象映射到类属性,如接口,typescript,Typescript,我喜欢在Typescript界面中执行以下操作: interface Person { name: string; age: number; } const person: Person = { name: "Foo"; age: 23; } 因为它非常精确和干净。然而,假设我需要添加一个对人有意义的方法isAdult。我无法向接口添加方法,因此需要将其转换为类: class Person { public name: string; public
interface Person {
name: string;
age: number;
}
const person: Person = {
name: "Foo";
age: 23;
}
因为它非常精确和干净。然而,假设我需要添加一个对人有意义的方法isAdult。我无法向接口添加方法,因此需要将其转换为类:
class Person {
public name: string;
public age: number;
isAdult() {
return this.age >= 18;
}
}
但是现在,我如何使用上面定义的object person实例化这个类呢?我不能这样做:
const person: Person = new Person({
name: "Foo";
age: 23;
})
我不想用构造函数参数和每个属性的显式集合显式地编写包含所有详细信息的类构造函数,也不想按照该顺序显式地传递PersonFoo,23,而不显式地说明什么是Foo,什么是23
那么,有没有一种方法可以从Person对象实例化Person类,并且尽可能简洁?这里可以做的是将类扩展到接口
class Person {
isAdult(age) {
return age >= 18;
}
}
interface IPerson extends Person {
name: string;
age: number;
}
然后可以使用IPerson接口创建变量
class Person {
isAdult(age) {
return age >= 18;
}
}
interface IPerson extends Person {
name: string;
age: number;
}
有关更多信息,请查看此处的文档。
或者,您可以在接口中创建一个函数声明,然后在类中定义该函数逻辑
interface IPerson extends Person {
name: string;
age: number;
isAdult():boolean;
}
这里可以做的是将类扩展到接口
class Person {
isAdult(age) {
return age >= 18;
}
}
interface IPerson extends Person {
name: string;
age: number;
}
然后可以使用IPerson接口创建变量
class Person {
isAdult(age) {
return age >= 18;
}
}
interface IPerson extends Person {
name: string;
age: number;
}
有关更多信息,请查看此处的文档。
或者,您可以在接口中创建一个函数声明,然后在类中定义该函数逻辑
interface IPerson extends Person {
name: string;
age: number;
isAdult():boolean;
}
你在向上游游了一点。:-但是你已经排除了通常的事情,那就是构造器的公共名称:string,公共年龄:number或类似的,所以 只是为了设置阶段,没有办法只将普通对象分配给具有类类型的变量,并将其连接到类的原型方法。您需要调用构造函数。例如,你需要一个新人。另一方面,您不需要声明类型;TypeScript将推断出它。所以它几乎同样简洁,你只需要输入new和几个[而不需要:] 你说过: 我不能这样做:
const person: Person = new Person({
name: "Foo";
age: 23;
})
const person:person=新的person{
姓名:富,;
年龄:23岁;;
}
我不想用构造函数参数和每个属性的显式集合来显式编写类构造函数
…但这样做并不需要编写显式设置属性的构造函数
系好安全带,这有点费劲-
如果所有属性都有初始值设定项
在这种情况下,它可以简单到:
constructor(props?: Partial<Person>) {
Object.assign(this, props);
}
该构造函数检查您传入的内容,这将被拒绝:
const p2 = new Person({
frog: "Foo", // // Argument of type '{ frog: string; age: number; }' is not assignable to parameter of type 'Partial<Person>'.
age: 23,
});
然后,Person等人分类:
class Person extends Base<Person> {
name!: string;
age!: number;
isAdult() {
return this.age >= 18;
}
}
无效属性失败:
const p2 = new Person({
frog: "Foo", // Argument of type '{ frog: string; age: number; }' is not assignable to parameter of type 'Pick<Person, NotKeyOfType<Person, Function>>'.
age: 23,
});
const p3 = new Person({
age: 23, // Argument of type '{ age: number; }' is not assignable to parameter of type 'Pick<Person, NotKeyOfType<Person, Function>>'.
// Property 'name' is missing in type '{ age: number; }' but required in type 'Pick<Person, NotKeyOfType<Person, Function>>'.
});
缺少的属性将失败:
const p2 = new Person({
frog: "Foo", // Argument of type '{ frog: string; age: number; }' is not assignable to parameter of type 'Pick<Person, NotKeyOfType<Person, Function>>'.
age: 23,
});
const p3 = new Person({
age: 23, // Argument of type '{ age: number; }' is not assignable to parameter of type 'Pick<Person, NotKeyOfType<Person, Function>>'.
// Property 'name' is missing in type '{ age: number; }' but required in type 'Pick<Person, NotKeyOfType<Person, Function>>'.
});
你在向上游游了一点。:-但是你已经排除了通常的事情,那就是构造器的公共名称:string,公共年龄:number或类似的,所以 只是为了设置阶段,没有办法只将普通对象分配给具有类类型的变量,并将其连接到类的原型方法。您需要调用构造函数。例如,你需要一个新人。另一方面,您不需要声明类型;TypeScript将推断出它。所以它几乎同样简洁,你只需要输入new和几个[而不需要:] 你说过: 我不能这样做:
const person: Person = new Person({
name: "Foo";
age: 23;
})
const person:person=新的person{
姓名:富,;
年龄:23岁;;
}
我不想用构造函数参数和每个属性的显式集合来显式编写类构造函数
…但这样做并不需要编写显式设置属性的构造函数
系好安全带,这有点费劲-
如果所有属性都有初始值设定项
在这种情况下,它可以简单到:
constructor(props?: Partial<Person>) {
Object.assign(this, props);
}
该构造函数检查您传入的内容,这将被拒绝:
const p2 = new Person({
frog: "Foo", // // Argument of type '{ frog: string; age: number; }' is not assignable to parameter of type 'Partial<Person>'.
age: 23,
});
然后,Person等人分类:
class Person extends Base<Person> {
name!: string;
age!: number;
isAdult() {
return this.age >= 18;
}
}
无效属性失败:
const p2 = new Person({
frog: "Foo", // Argument of type '{ frog: string; age: number; }' is not assignable to parameter of type 'Pick<Person, NotKeyOfType<Person, Function>>'.
age: 23,
});
const p3 = new Person({
age: 23, // Argument of type '{ age: number; }' is not assignable to parameter of type 'Pick<Person, NotKeyOfType<Person, Function>>'.
// Property 'name' is missing in type '{ age: number; }' but required in type 'Pick<Person, NotKeyOfType<Person, Function>>'.
});
缺少的属性将失败:
const p2 = new Person({
frog: "Foo", // Argument of type '{ frog: string; age: number; }' is not assignable to parameter of type 'Pick<Person, NotKeyOfType<Person, Function>>'.
age: 23,
});
const p3 = new Person({
age: 23, // Argument of type '{ age: number; }' is not assignable to parameter of type 'Pick<Person, NotKeyOfType<Person, Function>>'.
// Property 'name' is missing in type '{ age: number; }' but required in type 'Pick<Person, NotKeyOfType<Person, Function>>'.
});
Object.assignnewperson,{name:Foo,age:23}?Object.assignnewperson,{name:Foo,age:23}?示例用法是什么?const p=…?注意上面的类中有一个错误。类型“Person”上不存在属性“age”。示例用法是什么?const p=…?注意上面的类中有一个错误。属性“age”在类型“Person”上不存在。如果您在所有属性部分都有初始值设定项,则肯定是我要找的@江户-太好了!但我很高兴也把另一部分弄明白了-DYeah,虽然我现在的项目中不需要它,但这对未来来说是一个非常好的参考-如果你在所有的属性部分都有初始值设定项,那绝对是我想要的@江户-太好了!但我很高兴也把另一部分弄明白了-DYeah,虽然我现在的项目中不需要它,但这对未来来说是一个非常好的参考-