Search code examples
typescriptdestructuringfunction-parameter

Include type information when dereferencing?


When dereferencing an argument within a function call like this:

worker.onmessage = ({ data }) => 

Is there a way to include typing information?

For example in this case the dereferenced data object is a ParseResult. Can we include that somehow within the dereferencing syntax?

The end goal is to get autocomplete working within the function.

I could do something like:

const result:ParseResult = data

But I'm curious as to whether there is a shorter more sugared approach?


Solution

  • Unfortunately one cannot put types directly on the individual identifiers in the destructuring pattern, one can only type the whole parameter:

    worker.onmessage = ({ data }: { data: ParseResult }) =>