69 typescript
我有以下界面和代码.我以为我正在做正确的定义,但我收到一个错误:
interface IenumServiceGetOrderBy { id: number; label: string; key: any }[];
Run Code Online (Sandbox Code Playgroud)
和:
getOrderBy = (entity): IenumServiceGetOrderBy => {
var result: IenumServiceGetOrderBy;
switch (entity) {
case "content":
result =
[
{ 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'] }
];
break;
}
return result;
};
Run Code Online (Sandbox Code Playgroud)
错误:
Error 190 Cannot convert '{}[]' to 'IenumServiceGetOrderBy':
Type '{}[]' is missing property 'id' from type 'IenumServiceGetOrderBy'
Run Code Online (Sandbox Code Playgroud)
bas*_*rat 164
您不需要使用索引器(因为它的类型安全性稍差).您有两种选择:
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'] }
]
Run Code Online (Sandbox Code Playgroud)
我个人推荐选项A(当你使用类而不是接口时更简单的迁移).
Dou*_*las 71
您可以使用索引器定义接口:
interface EnumServiceGetOrderBy {
[index: number]: { id: number; label: string; key: any };
}
Run Code Online (Sandbox Code Playgroud)
NoN*_*ded 59
您只需扩展Array接口即可将接口定义为数组.
export interface MyInterface extends Array<MyType> { }
Run Code Online (Sandbox Code Playgroud)
有了这个,任何实现它的对象MyInterface都需要实现数组的所有函数调用,并且只能存储具有该MyType类型的对象.
Dan*_*wer 34
额外的简单选项:
interface simpleInt {
id: number;
label: string;
key: any;
}
type simpleType = simpleInt[];
Run Code Online (Sandbox Code Playgroud)
Jer*_*ett 24
不使用
interface EnumServiceGetOrderBy {
[index: number]: { id: number; label: string; key: any };
}
Run Code Online (Sandbox Code Playgroud)
您将收到所有 Arrays 属性和方法(例如 splice 等)的错误。
解决方案是创建一个接口,该接口定义另一个接口的数组(将定义对象)
例如:
interface TopCategoriesProps {
data: Array<Type>;
}
interface Type {
category: string;
percentage: number;
}
Run Code Online (Sandbox Code Playgroud)
Jso*_*ody 22
假设您想要一个Workplace充满 s 的类型Person。你需要这样的东西:
interface Workplace {
name: string
age: number
}[]
// that [] doesn't work!! ;)
Run Code Online (Sandbox Code Playgroud)
这里的问题是接口用于指定类/对象形状..仅此而已。所以你可以用typewitch来代替,这样更灵活
type而不是interface
type Workplace = {
name: string
age: number
}[]
// This works :D
Run Code Online (Sandbox Code Playgroud)
Person,interface然后由人员数组创建 - Workplace>typePerson[]
interface Person {
name: string
age: number
}
type Workplace = Person[]
// nice ;)
Run Code Online (Sandbox Code Playgroud)
如果您不想创建全新类型,这里有一个内联版本:
export interface ISomeInterface extends Array<{
[someindex: string]: number;
}> { };
Run Code Online (Sandbox Code Playgroud)
小智 8
在 Angular 中,使用“扩展”来定义对象“数组”的接口。
使用索引器会给你一个错误,因为它不是一个数组接口,所以不包含属性和方法。
例如
错误 TS2339:类型“ISelectOptions2”上不存在属性“find”。
// good
export interface ISelectOptions1 extends Array<ISelectOption> {}
// bad
export interface ISelectOptions2 {
[index: number]: ISelectOption;
}
interface ISelectOption {
prop1: string;
prop2?: boolean;
}
Run Code Online (Sandbox Code Playgroud)
像这样使用!
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,
},
]
Run Code Online (Sandbox Code Playgroud)
简单的选择,没有tslint错误...
export interface MyItem {
id: number
name: string
}
export type MyItemList = [MyItem]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
108010 次 |
| 最近记录: |