[英]vanilla js & typescript event referencing
I would like to ask if there is any way to annotate the type of the e
parameter in this code:请问有没有什么办法可以注释这段代码中
e
参数的类型:
const onDrag = () => {
// Input element
const range = document.querySelector('input') as HTMLInputElement;
// Paragrahp element
const rangeValue = document.querySelector('#used-data') as HTMLElement;
// THIS ONE HERE
range.addEventListener('change', (e) => {
rangeValue.textContent = `${e} GB`;
});
};
onDrag();
Btw, the range
variable is a range input element.顺便说一句,
range
变量是一个范围输入元素。 The inferred annotation of e
is an event
and it seems that I cant access the value using e.target.value
. e
的推断注释是一个event
,似乎我无法使用e.target.value
访问该值。 Aside from assigning the type of e
as any
or by using function() this.value
, is there another way to do this?除了将
e
的类型分配为any
或使用function() this.value
,还有其他方法可以做到这一点吗?
Typescript has a pretty good and straight forward online documentation which could be found here . Typescript 有一个非常好的和直接的在线文档,可以在这里找到。
In general, types are annotates by passing a colon and the variable type afterwards.通常,类型是通过在后面传递一个冒号和变量类型来注释的。 The events "change" and "input" are both typed as
InputEvent
.事件“change”和“input”都被输入为
InputEvent
。 Your code will look like the following:您的代码将如下所示:
range.addEventListener('change', (e: InputEvent) => {
rangeValue.textContent = `${e.data} GB`;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.