Skip to content

TS1057: Type null is not an awaitable return type #14031

@lukas-zech-software

Description

@lukas-zech-software

TypeScript Version: 2.1.6

Code

const MISSING_DATA: null = null;

export class Test {
  public async get(): Promise<number|null> {
    const result = await Promise.resolve(null);
    if (result === null) {

      //error TS1059: Return expression in async function does not have a valid callable 'then' member.
      return MISSING_DATA;

      //error TS1059: Return expression in async function does not have a valid callable 'then' member.
      return Promise.resolve<null>(MISSING_DATA);

      // compiles
      return Promise.resolve<number|null>(MISSING_DATA);
    }

    return Promise.resolve(1);
  }
}

Expected behavior:
Code compiles

Actual behavior:
Error error TS1059: Return expression in async function does not have a valid callable 'then' member. is thrown when returning a variable of type null

We use a constant value to indicate missing/invalid values on our data.
This constants is described with const MISSING_DATA: null = null;

When trying to return this value in a async method with the return type Promise<number|null> the mentioned error appears.

Why is null not an accepted return type for promises/async functions?

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions