TypeScript: casting HTMLElement

后端 未结 13 2311
我寻月下人不归
我寻月下人不归 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:40

    As of TypeScript 0.9 the lib.d.ts file uses specialized overload signatures that return the correct types for calls to getElementsByTagName.

    This means you no longer need to use type assertions to change the type:

    // No type assertions needed
    var script: HTMLScriptElement = document.getElementsByTagName('script')[0];
    alert(script.type);
    

提交回复
热议问题