Skip to content

type '() => void' should be assignable to type '(f32) => void' #1961

Open
@trusktr

Description

@trusktr

I forget the term for function parameter type assignability being reversed from variable assignability. Seems whatever that is called is not happening here.

This TypeScript playground shows how it should work.

But in AS, a function type like () => void is not assignable to one like (i: i32) => void, although it should be fine (it is safe, because the type without any parameters can not do anything wrong with a passed argument.

But maybe I'm missing something: does WebAssembly allow passing an arg to a function without parameters (only to be ignored)? If so, then it should be fine to make it so in AS.

Input:

declare function foo(f: (t: i32) => void): void

export function fib(n: i32): i32 {
  const f: () => void = () => {}

  foo(f) // error

  return 123
}

playground

Error:

ERROR TS2322: Type '() => void' is not assignable to type '(f32) => void'.

The error can be bypassed in certain cases, and the application will work fine:

declare function foo(f: (t: i32) => void): void
declare function log(s: string): void

export function fib(n: i32): i32 {
  foo(() => {
    log('test')
  }) // no error, example Runs fine!

  return 123
}

playground

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions