我有一个简单的模型类
export interface Category {
name: string;
description: string;
}
我需要在 Angular 组件中声明并初始化一个变量。尝试过:
category: Category = {};
错误: {} 无法分配到一个类别
category: Category = new Category();
错误:类别指的是一种类型,但被指定为值。
有什么建议吗?
我有一个简单的模型类
export interface Category {
name: string;
description: string;
}
我需要在 Angular 组件中声明并初始化一个变量。尝试过:
category: Category = {};
错误: {} 无法分配到一个类别
category: Category = new Category();
错误:类别指的是一种类型,但被指定为值。
有什么建议吗?
解决这个问题有多种方法,具体取决于您的期望结果。
interface
转换为一个class
export class Category {
name: string;
description: string;
}
const category: Category = new Category();
接口
扩展为类
export class CategoryObject implements Category {
}
const category: Category = new CategoryObject();
interface
匹配const category: Category = {
name: 'My Category',
description: 'My Description',
};
export interface Category {
name?: string;
description?: string;
}
const category: Category = {};
Partial<T>
export interface Category {
name: string;
description: string;
}
const category: Partial<Category> = {};
如果你不想把你的定义从interface
改为class
,你也可以这样做:
let category = <Category>{ };
否则,您可以按照其他答案的建议,将您的 Category
更改为类。
编辑: 根据下面ruffin的评论,如果界面是export interface ITiered { one: { two: { three: function (x) {...} } } }
如果您尝试let x = {} as ITiered
,然后调用像x.one.two.three()
这样的内容时,将会出现错误。
export interface ITiered { one: { two: { three: function (x) {...} } } }
并且您尝试 let x = {} as ITiered
,那么 x.one.two.three()
不会在没有定义 two
的情况下崩溃吗?我想我应该设置一个最简单的情况... ;^) - ruffin<GenericType>
意味着一个泛型类型。但是仅仅指一个泛型类型,具体意思是什么? - Jimmy Chu<Category>{}
语法是否可行? - friederbluemle在Typescript中,如果你想使用对象初始化器,你需要在类中定义所有属性。
let category: Category = {
name: '',
description: ''
};
这种方法可以使您的模型仍保持为接口。
export interface Category {
name: string;
description: string;
}
category = {} as Category ;
编辑:想把下面的评论放在答案中,这样人们就知道我的答案为什么是错误的。我假设你总是会立即分配,但这不是一个好的假设。
现在你的系统中有一个被认为是某种类型,但缺少一些必要属性的对象。这只会导致在某个地方出错,是错误的建议。
类似于 C# 的类:
export class Category {
category: number = null;
description: string = null;
name: string = null;
public constructor(init?: Partial<Category>) {
Object.assign(this, init);
}
}
const instance_of_category: Category = new Category();
现在您拥有一个空的类对象,其中所有字段都已定义,就像C#一样:
instance_of_category{
"category": null,
"description": null,
"name": null
}
您的对象字面量必须与接口匹配。由于您的接口有两个必需属性(name
和description
),因此在实例化对象时必须同时声明它们。
const category: Category = {
name: 'foo',
description: 'bar'
};
如果你不能一开始就构建整个对象,你可以使用内置的 Partial
类型来构建对象。
const builder: Partial<Category> = {};
builder.name = 'foo';
builder.description = 'bar';
const category: Category = builder as Category;
return Object.assign(new Person(), {
name:"Your Name"
});
interface Person{
id: number;
name: string;
}
let x: Person = {
id : 0,
name :"JOHN"
};
alert(x.name);
let category = <Category>{ };
当您不想更改接口或类的定义时,可以采用这种方式。
通过这种方式,将创建一个通用对象类型,它不依赖于数据类型,并且可重复使用。
您也可以使用Record
类型。
{
category: Record<string, string>;
}
const category: Category = {} as any
。我不建议这样做,也不会将其添加到我的答案中。 - Ian MacDonaldlet category = <Category>{ };
(仅当您确信要添加所有属性时)。 - RamoFXPartial
初始化对象是否正确?例如:const data: Partial<DataType> = {};
- Caio Marconst data = <DataType>{};
-- 两种方式都正确吗? - Caio Mar