Property 'value' does not exist on type EventTarget in TypeScript

后端 未结 8 1027
野趣味
野趣味 2020-11-29 17:06

So the following code is in Angular 4 and I can\'t figure out why it doesn\'t work the way as expected.

Here is a snippet of my handler:

onUpdatingSe         


        
相关标签:
8条回答
  • 2020-11-29 17:39

    You should use event.target.value prop with onChange handler if not you could see :

    index.js:1437 Warning: Failed prop type: You provided a `value` prop to a form field without an `onChange` handler. This will render a read-only field. If the field should be mutable use `defaultValue`. Otherwise, set either `onChange` or `readOnly`.
    

    Or If you want to use other handler than onChange, use event.currentTarget.value

    0 讨论(0)
  • 2020-11-29 17:40

    add any type to event

    event: any
    

    example

    [element].addEvenListener('mousemove', (event: any) =>{
    //CODE//
    } )
    

    what happens is that typescript adds event as Event type and for some reason it doesn't recognize some properties. Adding it of type any no longer exists this problem, this works for any document.[Property]

    0 讨论(0)
  • 2020-11-29 17:42

    Here's another fix that works for me:

    (event.target as HTMLInputElement).value

    That should get rid of the error by letting TS know that event.target is an HTMLInputElement, which inherently has a value. Before specifying, TS likely only knew that event alone was an HTMLInputElement, thus according to TS the keyed-in target was some randomly mapped value that could be anything.

    0 讨论(0)
  • 2020-11-29 17:56

    you can also create your own interface as well.

        export interface UserEvent {
          target: HTMLInputElement;
        }
    
           ...
    
        onUpdatingServerName(event: UserEvent) {
          .....
        }
    
    0 讨论(0)
  • 2020-11-29 17:58

    event.target here is an HTMLElement which is the parent of all HTML elements, but isn't guaranteed to have the property value. TypeScript detects this and throws the error. Cast event.target to the appropriate HTML element to ensure it is HTMLInputElement which does have a value property:

    (<HTMLInputElement>event.target).value
    

    Per the documentation:

    Type the $event

    The example above casts the $event as an any type. That simplifies the code at a cost. There is no type information that could reveal properties of the event object and prevent silly mistakes.

    [...]

    The $event is now a specific KeyboardEvent. Not all elements have a value property so it casts target to an input element.

    (Emphasis mine)

    0 讨论(0)
  • 2020-11-29 18:01

    The way I do it is the following (better than type assertion imho):

    onFieldUpdate(event: { target: HTMLInputElement }) {
      this.$emit('onFieldUpdate', event.target.value);
    }
    

    This assumes you are only interested in the target property, which is the most common case. If you need to access the other properties of event, a more comprehensive solution involves using the & type intersection operator:

    event: Event & { target: HTMLInputElement }
    

    This is a Vue.js version but the concept applies to all frameworks. Obviously you can go more specific and instead of using a general HTMLInputElement you can use e.g. HTMLTextAreaElement for textareas.

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