반응형
Typescript로 객체 배열에 대한 인터페이스를 어떻게 정의 할 수 있습니까?
다음 인터페이스와 코드가 있습니다. 정의를 올바르게 수행하고 있다고 생각했지만 오류가 발생합니다.
interface IenumServiceGetOrderBy { id: number; label: string; key: any }[];
과:
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;
};
오류:
Error 190 Cannot convert '{}[]' to 'IenumServiceGetOrderBy':
Type '{}[]' is missing property 'id' from type 'IenumServiceGetOrderBy'
인덱서를 사용하여 인터페이스를 정의 할 수 있습니다 .
interface EnumServiceGetOrderBy {
[index: number]: { id: number; label: string; key: any };
}
인덱서를 사용할 필요 가 없습니다 (조금 덜 형식 안전하기 때문에). 두 가지 옵션이 있습니다.
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'] }
]
개인적으로 옵션 A를 권장합니다 (인터페이스가 아닌 클래스를 사용하는 경우 더 간단한 마이그레이션).
단순히 Array 인터페이스를 확장하여 인터페이스를 배열로 정의 할 수 있습니다.
export interface MyInterface extends Array<MyType> { }
이를 통해를 구현하는 모든 객체 MyInterface
는 배열의 모든 함수 호출을 구현해야하며 MyType
유형 이있는 객체 만 저장할 수 있습니다 .
tslint 오류가없는 쉬운 옵션 ...
export interface MyItem {
id: number
name: string
}
export type MyItemList = [MyItem]
또한 이것을 할 수 있습니다.
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'] }
];
추가 쉬운 옵션 :
interface simpleInt {
id: number;
label: string;
key: any;
};
type simpleType = simpleInt[];
Here's an inline version if you don't want to create a whole new type:
export interface ISomeInterface extends Array<{
[someindex: string]: number;
}> { };
In Angular use 'extends' to define the interface for an 'Array' of objects.
Using an indexer will give you an error as its not an Array interface so doesn't contain the properties and methods.
e.g
error TS2339: Property 'find' does not exist on type 'ISelectOptions2'.
// good
export interface ISelectOptions1 extends Array<ISelectOption> {}
// bad
export interface ISelectOptions2 {
[index: number]: ISelectOption;
}
interface ISelectOption {
prop1: string;
prop2?: boolean;
}
반응형
'Nice programing' 카테고리의 다른 글
xcrun 개발자 경로 변경 (0) | 2020.11.25 |
---|---|
gem을 사용하여 Ubuntu에 Rails를 설치하는 방법 (0) | 2020.11.25 |
서버가 이미 실행 중입니다. (0) | 2020.11.25 |
Android 진행률 표시 줄의 높이를 설정하는 방법은 무엇입니까? (0) | 2020.11.25 |
프로그래밍 방식으로 SearchView를 닫는 방법은 무엇입니까? (0) | 2020.11.25 |