typescript版本中缺少文档类中的函数

typescript版本中缺少文档类中的函数,typescript,Typescript,我们正在从javascript迁移到typescript,document类中的一些函数似乎已经不存在了。我找不到任何等价物 1) createRange() var textRange = document.getSelection().createRange(); 2)createTextRange var preCaretTextRange = document.body.createTextRange(); 3)focus()

我们正在从javascript迁移到typescript,document类中的一些函数似乎已经不存在了。我找不到任何等价物

 1) createRange()
          var textRange = document.getSelection().createRange();
 2)createTextRange
          var preCaretTextRange = document.body.createTextRange();
 3)focus()
        var el : Node= document.getElementById(divId).childNodes[0];
           el.focus();
(1)对象没有
createRange
方法(也可以在中看到)。
相反,您应该使用:

(2)
createTextRange
是IE唯一的东西(),它不是标准。
这其中有一个问题:它说:

这是一个突破,因为这是一个只支持IE的API。用户可以增加 必要时提供接口

所以你可以这样做:

interface TextRange {
    ...
}

interface HtmlElement {
    createTextRange(): TextRange;
}
(3)
focus()
部分工作正常,编译器没有问题


编辑 节点对象没有
focus()
方法,因此需要强制转换它:

var el = document.getElementById(divId).childNodes[0] as HTMLElement;
el.focus(); // works now
(1) 对象没有
createRange
方法(也可以在中看到)。
相反,您应该使用:

(2)
createTextRange
是IE唯一的东西(),它不是标准。
这其中有一个问题:它说:

这是一个突破,因为这是一个只支持IE的API。用户可以增加 必要时提供接口

所以你可以这样做:

interface TextRange {
    ...
}

interface HtmlElement {
    createTextRange(): TextRange;
}
(3)
focus()
部分工作正常,编译器没有问题


编辑 节点对象没有
focus()
方法,因此需要强制转换它:

var el = document.getElementById(divId).childNodes[0] as HTMLElement;
el.focus(); // works now

非常感谢。focus()问题仍然存在,我已经更新了问题。如何解决问题#2?检查我关于
焦点的修订答案,至于第二个问题,因为引用说你需要“增强接口”,我给了你一个简短的片段作为如何解决的示例,我被引导到这个答案,但我仍然得到编译错误,例如,
类型为“Range”的参数不能分配给类型为“TextRange”的参数。
@yondaimehokage没有类型为
TextRange
,您是否如答案所示自己添加了它?谢谢!focus()问题仍然存在,我已经更新了问题。如何解决问题#2?检查我关于
焦点的修订答案,至于第二个问题,因为引用说你需要“增强接口”,我给了你一个简短的片段作为如何解决的示例,我被引导到这个答案,但我仍然得到编译错误,例如,
类型为“Range”的参数不能分配给类型为“TextRange”的参数。
@yondaimehokage没有类型为
TextRange
,您是否如答案所示自己添加了它?