如何使用Typescript为对象数组定义接口?
我有以下接口和代码。我认为我的定义是正确的,但我得到了一个错误:如何使用Typescript为对象数组定义接口?,typescript,Typescript,我有以下接口和代码。我认为我的定义是正确的,但我得到了一个错误: interface IenumServiceGetOrderBy { id: number; label: string; key: any }[]; 以及: 错误: Error 190 Cannot convert '{}[]' to 'IenumServiceGetOrderBy': Type '{}[]' is missing property 'id' from type 'IenumServiceGetOr
interface IenumServiceGetOrderBy { id: number; label: string; key: any }[];
以及:
错误:
Error 190 Cannot convert '{}[]' to 'IenumServiceGetOrderBy':
Type '{}[]' is missing property 'id' from type 'IenumServiceGetOrderBy'
您可以使用以下命令定义接口: 您不需要使用索引器(因为它不太安全)。您有两个选择:
interface EnumServiceItem {
id: number; label: string; key: any
}
interface EnumServiceItems extends Array<EnumServiceItem>{}
// Option A
var result: EnumServiceItem[] = [
{ id: 0, label: 'CId', key: 'contentId' },
{ id: 1, label: 'Modified By', key: 'modifiedBy' },
{ id: 2, label: 'Modified Date', key: 'modified' },
{ id: 3, label: 'Status', key: 'contentStatusId' },
{ id: 4, label: 'Status > Type', key: ['contentStatusId', 'contentTypeId'] },
{ id: 5, label: 'Title', key: 'title' },
{ id: 6, label: 'Type', key: 'contentTypeId' },
{ id: 7, label: 'Type > Status', key: ['contentTypeId', 'contentStatusId'] }
];
// Option B
var result: EnumServiceItems = [
{ id: 0, label: 'CId', key: 'contentId' },
{ id: 1, label: 'Modified By', key: 'modifiedBy' },
{ id: 2, label: 'Modified Date', key: 'modified' },
{ id: 3, label: 'Status', key: 'contentStatusId' },
{ id: 4, label: 'Status > Type', key: ['contentStatusId', 'contentTypeId'] },
{ id: 5, label: 'Title', key: 'title' },
{ id: 6, label: 'Type', key: 'contentTypeId' },
{ id: 7, label: 'Type > Status', key: ['contentTypeId', 'contentStatusId'] }
]
接口枚举服务项{
id:编号;标签:字符串;键:任意
}
接口EnumServiceItems扩展数组{}
//方案A
变量结果:EnumServiceItem[]=[
{id:0,标签:'CId',键:'contentId'},
{id:1,标签:'Modified By',键:'modifiedBy'},
{id:2,标签:'Modified Date',键:'Modified'},
{id:3,标签:'Status',键:'contentStatusId'},
{id:4,标签:'Status>Type',键:['contentStatusId','contentTypeId']},
{id:5,标签:'Title',键:'Title'},
{id:6,标签:'Type',键:'contentTypeId'},
{id:7,标签:'Type>Status',键:['contentTypeId','contentStatusId']}
];
//方案B
变量结果:EnumServiceItems=[
{id:0,标签:'CId',键:'contentId'},
{id:1,标签:'Modified By',键:'modifiedBy'},
{id:2,标签:'Modified Date',键:'Modified'},
{id:3,标签:'Status',键:'contentStatusId'},
{id:4,标签:'Status>Type',键:['contentStatusId','contentTypeId']},
{id:5,标签:'Title',键:'Title'},
{id:6,标签:'Type',键:'contentTypeId'},
{id:7,标签:'Type>Status',键:['contentTypeId','contentStatusId']}
]
就我个人而言,我推荐选项A(使用类而不是接口时更简单的迁移) 只需扩展数组接口,即可将接口定义为数组
export interface MyInterface extends Array<MyType> { }
导出接口MyInterface扩展数组{}
这样,任何实现
MyInterface
的对象都需要实现数组的所有函数调用,并且只能存储具有MyType
类型的对象。轻松选项,没有tslint错误
export interface MyItem {
id: number
name: string
}
export type MyItemList = [MyItem]
如果您不想创建一个全新的类型,这里有一个内联版本:
export interface ISomeInterface extends Array<{
[someindex: string]: number;
}> { };
导出接口扩展数组{};
您也可以这样做
interface IenumServiceGetOrderBy {
id: number;
label: string;
key: any;
}
// notice i am not using the []
var oneResult: IenumServiceGetOrderBy = { id: 0, label: 'CId', key: 'contentId'};
//notice i am using []
// it is read like "array of IenumServiceGetOrderBy"
var ArrayOfResult: IenumServiceGetOrderBy[] =
[
{ id: 0, label: 'CId', key: 'contentId' },
{ id: 1, label: 'Modified By', key: 'modifiedBy' },
{ id: 2, label: 'Modified Date', key: 'modified' },
{ id: 3, label: 'Status', key: 'contentStatusId' },
{ id: 4, label: 'Status > Type', key: ['contentStatusId', 'contentTypeId'] },
{ id: 5, label: 'Title', key: 'title' },
{ id: 6, label: 'Type', key: 'contentTypeId' },
{ id: 7, label: 'Type > Status', key: ['contentTypeId', 'contentStatusId'] }
];
其他简单选项:
接口简化{
id:编号;
标签:字符串;
键:任意;
}
键入simpleType=simpleInt[];
在Angular中,使用“extends”定义对象“Array”的接口
使用索引器将导致错误,因为它不是数组接口,因此不包含属性和方法
e、 g
错误TS2339:类型“ISelectOptions2”上不存在属性“find”
//很好
导出接口ISelectOptions1扩展数组{}
//坏的
导出接口ISelectOptions 2{
[索引:编号]:i选择选项;
}
接口ISelectOption{
prop1:字符串;
prop2?:布尔型;
}
编程很简单。使用简单用例:
interface IenumServiceGetOrderBy { id: number; label: string; key: any }
// OR
interface IenumServiceGetOrderBy { id: number; label: string; key: string | string[] }
// use interface like
const result: IenumServiceGetOrderBy[] =
[
{ id: 0, label: 'CId', key: 'contentId' },
{ id: 1, label: 'Modified By', key: 'modifiedBy' },
{ id: 4, label: 'Status > Type', key: ['contentStatusId', 'contentTypeId'] }
];
不要使用
interface EnumServiceGetOrderBy {
[index: number]: { id: number; label: string; key: any };
}
您将获得所有数组属性和方法(如splice等)的错误
解决方案是创建一个接口,该接口定义另一个接口(将定义对象)的数组
例如:
interface TopCategoriesProps {
data: Array<Type>;
}
interface Type {
category: string;
percentage: number;
}
接口TopCategoriesProps{
数据:数组;
}
接口类型{
类别:字符串;
百分比:数字;
}
以下是一个适合您的示例的解决方案:
interface IenumServiceGetOrderByAttributes {
id: number;
label: string;
key: any
}
interface IenumServiceGetOrderBy extends Array<IenumServiceGetOrderByAttributes> {
}
使用此解决方案,您可以使用阵列的所有属性和方法(例如:
length,push(),pop(),splice()
…)像这样使用
interface Iinput {
label: string
placeholder: string
register: any
type?: string
required: boolean
}
// This is how it can be done
const inputs: Array<Iinput> = [
{
label: "Title",
placeholder: "Bought something",
register: register,
required: true,
},
]
接口输入{
标签:字符串
占位符:字符串
登记:有吗
类型?:字符串
必填项:布尔值
}
//这就是可以做到的
常量输入:数组=[
{
标签:“标题”,
占位符:“买了东西”,
注册:注册,
要求:正确,
},
]
不是真正的“Typescript对象数组接口”是的,您仅为特定项目定义接口的示例将是一种更有用的方法。很少有项目数组,但不想方便地引用单个项目。使用真实数组也会暴露接口上的.length,这可能会经常使用。如何定义类来实现此接口?此解决方案的问题是这不是数组接口,因此,所有Array
属性和方法(如length
或push
)都将丢失。这样,在使用数组函数时,您将最终出错。请参见-正在将?
添加到接口中的所有声明中,例如id?:number;标签?:字符串;键?:如果使用的属性少于界面中定义的属性,那么任何
避免强制转换问题的最佳方法?例如,我可能不会在键入的数组设置中指定键
。选项B很有用,因为它允许您在编写更高级别的界面时使用该界面。Geezus christ这让我困扰了这么久,谢谢!第二个接口声明给出了以下tslint错误:“声明没有成员的接口等价于其超类型”我可以建议'key'的类型是string | string[],而不是any。如下所示:interface EnumServiceItem{id:number;label:string;key:string | string[]}
导出的MyItemList
类型将是“MyItem
的一个元素数组”,可能是导出类型MyItemList=MyItem[]
?。在我看来,接口和类型别名的组合最适合这种情况,因为您的类型别名解决方案中有所有数组类型的方法。这应该是可以接受的答案。为什么不使用类型(type MyTypeArray=Array
)而不是扩展接口呢?OP询问接口,我假设它是用其他属性和方法扩展的,您不能使用类型来实现这一点,而且类型在通用性方面比接口更有限。最好的解决方案应该是接受的answer@PauFrac她现在连米都有了
interface TopCategoriesProps {
data: Array<Type>;
}
interface Type {
category: string;
percentage: number;
}
interface IenumServiceGetOrderByAttributes {
id: number;
label: string;
key: any
}
interface IenumServiceGetOrderBy extends Array<IenumServiceGetOrderByAttributes> {
}
let result: IenumServiceGetOrderBy;
interface Iinput {
label: string
placeholder: string
register: any
type?: string
required: boolean
}
// This is how it can be done
const inputs: Array<Iinput> = [
{
label: "Title",
placeholder: "Bought something",
register: register,
required: true,
},
]