This project is a fork of lib.es6.d.ts
of typescript aimed at making
DOM programming more fluent.
In this library, querySelector()
, getElementById()
, getElementsByClassName()
, children
, firstElementChild
etc. return UniversalElement
or a list of UniversalElement
, which inherits all known sub interfaces of Element
;
UIEvent::target
is typed as UniversalUIEventTarget
, which extends UniversalElement
, Document
, and Window
.
So in most case you don't have to cast the result before use anymore!
Additionally you don't loose the power of type check and autocompletion. Look at this snapshot:
This snapshot was taken from Visual Studio Code) and demostrates autocompletion of
HTMLInputElement::files
of the result of querySelectorAll()
.
You can also take test-dom.ts
as an example.
To use this library, you can reference this lib.es6.d.ts
file from your TS file, and pass --noLib
to tsc
(or use noLib
in tsconfig.json
file).
-
Some properties are typed as
any
because of type confliction. E.g.HTMLInputElement::value
isstring
, butHTMLProgressElement::value
isnumber
, soUniversalElement::value
becomesany
. Such properties are:x, y, width, height, href, value, rows, cols, dx, dy, method, type, target, max
. -
getElementsByTagName(name: string)
still returnNodeList
, notNodeListOf<UniversalElement>
We can't do this because specializedgetElementsByTagName()
's return super types ofUniversalElement
. -
SVGElement::className
is typed asstring
, but it should beSVGAnimatedString
. In this librarySVGElement::className
is inherited fromElement
, so it has typestring
; but in reality it is always aSVGAnimatedString
. So If you play withclassName
of SVG elements via this library, you will get errors at runtime. You may useset|getAttribute()
orclassList
instead. By the way,SVGElement::className
is deprecated in SVG2 spec, so you should avoid using it anyway.