Skip to content

Compatibility error when callback has void return type #38284

Closed
@sosuisen

Description

@sosuisen

TypeScript Version: 3.8.3

Search Terms:
callback return type void

Code

const a: () => number = (): void => {};  // Type error. It is expected.
const b: () => void = (): number => { return 7 };  // Ok. It is expected.

a and b are expected behaviors which have been discussed many times,
see #21674 (comment)

But the following examples are different.

const f: (x: () => number) => number = (y: () => void) => 7;  // Ok. It is expected.
const g: (x: () => void) => number = (y: () => 7) => 7; // Type error. It is not expected. It should be Ok.

According to the Assignment Compatibility,
f is also expected behavior, that is to say,
type '(() => void) => number' is assignable to '(() => number) => number'
because both types are object (function) type
and the parameter type in '(() => void) => number' is assignable to or from the corresponding parameter type in '(() => number) => number'.

Here, parameter type '() => void' is assignable from type '() => number'.

g is expected to be ok because parameter type '() => number' is assignable to type '() => void',
according to Assignment Compatibility which says that 'each parameter type in N is assignable to or from the corresponding parameter type in M'.

Expected behavior:
Assignment Compatibility document may be wrong or its implementation may be wrong.

I assume that both may be wrong, f should be type error and g should be Ok in the same way as a and b.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions