Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for asyncronous assertion functions and methods #48309

Closed
5 tasks done
Nokel81 opened this issue Mar 17, 2022 · 4 comments
Closed
5 tasks done

Add support for asyncronous assertion functions and methods #48309

Nokel81 opened this issue Mar 17, 2022 · 4 comments

Comments

@Nokel81
Copy link

Nokel81 commented Mar 17, 2022

Suggestion

πŸ” Search Terms

  • assertion
  • method
  • function
  • async
  • promise

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Allow functions and methods of the signature (...args: any[]) => Promise<asserts condition> which mean that condition is true if the promise resolves.

πŸ“ƒ Motivating Example

Sometimes it is necessary to call asynchronous functions to make sure that some field is assigned. For instance, a somewhat common pattern is to have an async ensureFieldX(): Promise<void> method and it would be nice if typescript knew that after that method has resolved (and not rejected).

class Foo {
  private bar: string | undefined;

  async getBar(): Promise<string> {
    await this.ensureBar();

    return this.bar;
  }

  private async ensureBar(): Promise<asserts this is (Foo & { bar: string })> {
     ...
  }
}

πŸ’» Use Cases

See motivating example...

Could also be extended to asynchronous user defined type guards but I don't think that is as important.

@whzx5byb
Copy link

In this case you could use logical assignment as workaround:

class Foo {
  private bar: string | undefined;

  async getBar(): Promise<string> {
    return this.bar ||= await this.createBar();
  }

  private async createBar(): Promise<string> {
     //...
  }
}

@Nokel81
Copy link
Author

Nokel81 commented Mar 17, 2022

You could indeed but that is a bit cumbersome.

@MartinJohns
Copy link
Contributor

Duplicate of #37681.

@Nokel81
Copy link
Author

Nokel81 commented Mar 17, 2022

Sorry for the noise then I tried looking but didn't find that one.

@Nokel81 Nokel81 closed this as completed Mar 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants