TypeScript Version: 3.6.4
Code
document.querySelector('.page').offsetTop
This is correct. First of all, the result can be null, so if you have strictNullChecks enabled you need to perform a null check first:
const element = document.querySelector('.page');
if (element !== null) { ... }
Secondly, the return type is Element, and Element has no property offsetTop. You likely expect it to return a HTMLElement, but this is not obvious to the compiler because it could just as well be a SVGElement.
To fix this you can either use a type-assertion: element as HTMLElement
Or actively perform a runtime check: if (element instanceof HTMLElement) {}
Everything works as it should. Please also take note of the issue template.
Most helpful comment
This is correct. First of all, the result can be
null, so if you havestrictNullChecksenabled you need to perform anullcheck first:Secondly, the return type is
Element, andElementhas no propertyoffsetTop. You likely expect it to return aHTMLElement, but this is not obvious to the compiler because it could just as well be aSVGElement.To fix this you can either use a type-assertion:
element as HTMLElementOr actively perform a runtime check:
if (element instanceof HTMLElement) {}Everything works as it should. Please also take note of the issue template.