首页 前端知识 [ts]Property ‘screenWidth‘ does not exist on type ‘Window & typeof globalThis‘.ts(2339)

[ts]Property ‘screenWidth‘ does not exist on type ‘Window & typeof globalThis‘.ts(2339)

2025-03-09 15:03:29 前端知识 前端哥 86 732 我要收藏

[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 {};
转载请注明出处或者链接地址:https://www.qianduange.cn//article/23005.html
标签
评论
发布的文章

面试题之强缓存协商缓存

2025-03-11 15:03:21

【C语言】数组篇

2025-03-11 15:03:19

正则表达式(复习)

2025-03-11 15:03:17

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!