타이프 스크립트로 쓰고 모듈을 사용하지 않는 경우 글로벌 Window
개체를 확장 할 수 있습니다.타이프 스크립트 모듈 내에서 "창"확장
interface Window {
myCounter: number;
}
window.myCounter = window.myCounter || 0;
++window.myCounter;
function outputLoadingDetails() {
console.log(`myCounter= ${window.myCounter}`)
}
을하지만 export
와 기능 outputLoadingDetails
접두사 경우, 내가 모듈에이 파일을 변환하고 나타납니다 예를 들어,이 컴파일합니다. window.myCounter
에 액세스 할 때 컴파일러 오류가 발생합니다.
interface Window {
myCounter: number;
}
window.myCounter = window.myCounter || 0; // ERROR: property 'MyCounter' does not exist on type `Window`
++window.myCounter; // ERROR: property 'MyCounter' does not exist on type `Window`
export function outputLoadingDetails() {
console.log(`myCounter= ${window.myCounter}`) // ERROR: property 'MyCounter' does not exist on type `Window`
}
내 인터페이스 선언은 더 이상 글로벌 Window
유형을 확장하고 나타납니다.
해결 방법 한 가지 해결 방법은 인터페이스 선언을 별도의 *.d.ts
파일에 넣고이를 내 모듈에서 참조하는 것입니다.
하지만 지금은 궁금합니다. 모듈 코드 내에서 Window
인터페이스를 확장 할 수있는 방법이 있습니까?
아니, 그건 효과가 없을거야. 'type {new() : Window(); 프로토 타입 창; }은 Window 유형에 할당 할 수 없습니다. ' –