Typescript input onchange event.target.value

后端 未结 11 908
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 21:25

In my react and typescript app, I use: onChange={(e) => data.motto = (e.target as any).value}.

How do I correctly define the typings for the class, s

11条回答
  •  温柔的废话
    2020-11-28 22:05

    Thanks @haind

    Yes HTMLInputElement worked for input field

    //Example
    var elem = e.currentTarget as HTMLInputElement;
    elem.setAttribute('my-attribute','my value');
    elem.value='5';
    

    This HTMLInputElement is interface is inherit from HTMLElement which is inherited from EventTarget at root level. Therefore we can assert using as operator to use specific interfaces according to the context like in this case we are using HTMLInputElement for input field other interfaces can be HTMLButtonElement, HTMLImageElement etc.

    For more reference you can check other available interface here

    • Web API interfaces by Mozilla
    • Interfaces in External Node Modules by Microsoft

提交回复
热议问题