TypeScript中使用getBoundingClientRect報錯Property 'x' does not exist on type 'ClientRect | DOMRect

內容參考github ts問題11085
DomRect之前叫ClientRect,後來更名了1
ts兼容了兩種對象,但ClientRect沒有x,y屬性。所以使用getBoundingClientRect時:

let unspecifiedRect = document.body.getBoundingClientRect();
unspecifiedRect.x // fails
let domRect = document.body.getBoundingClientRect() as DOMRect;
domRect.x       // works

  1. ClientRect更名爲DomRect ↩︎

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章