인덱스 시그니처 속성
Last updated
Last updated
interface ButtonInterface {
onInit?():void;
onClick():void;
}
class ButtonComponent implements ButtonInterface {
type:string = 'button';
disabled:boolean = false;
constructor() {}
onClick() { console.log('버튼 클릭') }
}interface ButtonInterface {
onInit?():void;
onClick():void;
}
const button:ButtonInterface = {
// [오류]
// '{ type: string; disabled: boolean; onClick(): void; }' 형식은
// 'ButtonInterface' 형식에 할당할 수 없습니다. 개체 리터럴은 알려진 속성만
// 지정할 수 있으며 'ButtonInterface' 형식에 'type'이(가) 없습니다.
type: 'button',
disabled: false,
onClick() { console.log('버튼 클릭') }
};{
"compilerOptions": {
...,
"noImplicitAny": false, // 권장되지 않음
...
}
}interface ButtonInterface {
onInit?():void;
onClick():void;
// 인덱스 시그니처
[prop:string]: any;
}
const button:ButtonInterface = {
type: 'button',
disabled: false,
onClick() { console.log('버튼 클릭') }
};