Home > Software design >  Typescript function returntype
Typescript function returntype

Time:12-07

I have this function declaration inside af module:

declare module 'picoapp' {
  export function component(node?: HTMLElement): void
}

and then using it like this in a .ts file

export default component((node: HTMLElement) => {
   // All sorts of TS/JS here
})

But VSCode gives me this warning: Argument of type '(node: HTMLElement) => void' is not assignable to parameter of type 'HTMLElement'.

What should the returntype of the function then be? I'm not returning a value, but just using the node for reference.

CodePudding user response:

If the usage of your component function is correct, the type should be declared as following where callback can be whatever name you prefer.

declare module 'picoapp' {
    export function component(callback:(node?: HTMLElement)=>void): void
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related