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 객체에 새로운 속성을 추가할 수 있습니다.
'WEB개발 > REACT' 카테고리의 다른 글
[환경구성] React개발을 위한 유용한 노드모듈 (node_modules) (0) | 2025.02.14 |
---|---|
[환경구성] tsconfig.js, tsc (0) | 2025.02.13 |
[React] Next, Typescript... 오류모음 (0) | 2024.01.26 |
[React] web성능 메트릭 (0) | 2023.12.18 |
[React] Typescript (1) | 2023.12.18 |