[ts]Property ‘screenWidth‘ does not exist on type ‘Window & typeof globalThis‘.ts(2339)
problem
window.onresize = () => {
return (() => {
// ts报错:Property 'screenWidth' does not exist on type 'Window & typeof globalThis'.ts(2339)
window.screenWidth = document.body.clientWidth;
state.screenWidth = window.screenWidth;
})();
};
reason
ts不认识window下的screenWidth
solution
声明类型,根目录下的类型声明
// global.d.ts
declare global {
interface Window {
screenWidth:any;
}
}
export {};
文章讲述了在TypeScript中遇到`PropertyscreenWidthdoesnotexistontypeWindow&typeofglobalThis`的错误时,如何通过在全局类型声明文件(global.d.ts)中扩展Window接口来添加screenWidth属性,从而消除类型检查错误。
784

被折叠的 条评论
为什么被折叠?



