TypeScript: casting HTMLElement

后端 未结 13 2251
我寻月下人不归
我寻月下人不归 2020-12-02 05:34

Does anyone know how to cast in TypeScript?

I\'m trying to do this:

var script:HTMLScriptElement = document.getElementsByName(\"script\")[0];
alert(s         


        
13条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 05:46

    Just to clarify, this is correct.

    Cannot convert 'NodeList' to 'HTMLScriptElement[]'

    as a NodeList is not an actual array (e.g. it doesn't contain .forEach, .slice, .push, etc...).

    Thus if it did convert to HTMLScriptElement[] in the type system, you'd get no type errors if you tried to call Array.prototype members on it at compile time, but it would fail at run time.

提交回复
热议问题