How to use SVGDocument in Typescript?

前端 未结 2 1359
深忆病人
深忆病人 2021-01-15 05:46

According to the W3C recommendations, the method getSVGDocument()should return an SVGDocument, but the code in lib.d.ts is:

interfa         


        
相关标签:
2条回答
  • The typescript Document type is comparable to an SVGDocument, so you should be fine to just use Document. You will need to ensure that your element is casted as the right type (SVGElement or SVGSVGElement).

    Also, you can do double casting in Typescript. This may allow you to come up with a way to cast an element/document as any and back to the document/element type you need in the case that you run into further issues.

    0 讨论(0)
  • 2021-01-15 06:01

    According to the w3 spec SVGDocument implements Document and adds some stuff like title, referrer, rootElement and other stuff. Those are all defined on the Document interface for TypeScript so you can already use it without changing anything.

    The document.rootElement in TypeScript as in the w3 spec both return a SVGSVGElement.

    Because TypeScript uses structural typing (and the types match here) you can use it right away.

    edit: Because of the lib.d.ts is structured like this you do have the problem that you can do document.forms for example which you can't use on a SVGDocument which is a bit unfortunate because it does compile this way..

    0 讨论(0)
提交回复
热议问题