Skip to content

Class expression async method infers void return, rejecting return <value> (both modes) #776

@nickna

Description

@nickna

A class expression async method whose body returns a value is rejected because its return type is inferred as void. The equivalent class declaration infers the return correctly.

Repro

const C = class { async m() { return 5; } };
(new C().m() as Promise<number>).then(v => console.log(v));
Type Error: Type '5' is not assignable to type 'void'

Contrast — the class declaration form works (prints 5):

class C { async m() { return 5; } }
(new C().m() as Promise<number>).then(v => console.log(v));   // 5 ✓

Notes

  • Reproduces in both the interpreter and compiled mode (it is a type-checker inference gap, so it is independent of back end).
  • The class-expression method path does not infer a method's return type from its body the way the class-declaration path does — it defaults to void, so any return <value> then fails TS2322.
  • Found while scoping Compiled mode: computed symbol-keyed class method follow-ups (#647) #755 (compiled class-expression follow-ups). Independent of that change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions