WEB개발/REACT
React 참고, 오류
wooyeon06
2025. 2. 20. 15:12
Window객체 확장
declare global 구문을 사용하면 브라우저의 window 객체를 확장하는 인터페이스를 정의할 수 있습니다. TypeScript에서 Window 인터페이스는 전역 window 객체를 나타내므로, 이 인터페이스를 확장하면 브라우저 환경에서 window 객체에 새로운 속성을 추가할 수 있습니다.
declare global {
interface Window {
myCustomProperty: string;
myCustomFunction: () => void;
}
}
// window 객체에 새로운 속성을 추가
window.myCustomProperty = "Hello, world!";
window.myCustomFunction = () => {
console.log("Custom function called!");
};
// 사용 예제
console.log(window.myCustomProperty); // "Hello, world!"
window.myCustomFunction(); // "Custom function called!"
declare global 구문을 사용하면 브라우저의 window 객체를 확장하는 인터페이스를 정의할 수 있습니다. TypeScript에서 Window 인터페이스는 전역 window 객체를 나타내므로, 이 인터페이스를 확장하면 브라우저 환경에서 window 객체에 새로운 속성을 추가할 수 있습니다.