Home > Net >  What Type is Needed When Typescript Function Returns Method on Parameter
What Type is Needed When Typescript Function Returns Method on Parameter

Time:07-13

I have function which returns method on parameter and typescript says Error that my parameter has any type and I have tried using Function type but it still says error so which type is assignable to a something like this function? const PreventParentOnClick = (event) => event.stopPropagation()

(event: what should I type here ? )

CodePudding user response:

The type of the event should be React.MouseEvent<HTMLElement>.
Also, make sure that your function starts with a lowercase letter:

const preventParentOnClick = (event: React.MouseEvent<HTMLElement>) => { 
    event.stopPropagation()
}

CodePudding user response:

You can annotate the entire function as what it is, a MouseEventHandler. event will be inferred to be a MouseEvent.

const preventParentOnClick: MouseEventHandler<HTMLElement> = (event) => { 
    event.stopPropagation()
}
  • Related