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

Investigate how to support const app = fastify(); await app; in TypeScript #2378

Closed
Ethan-Arrowood opened this issue Jul 7, 2020 · 5 comments · Fixed by #2428
Closed

Investigate how to support const app = fastify(); await app; in TypeScript #2378

Ethan-Arrowood opened this issue Jul 7, 2020 · 5 comments · Fixed by #2428
Labels
good first issue Good for newcomers typescript TypeScript related

Comments

@Ethan-Arrowood
Copy link
Member

Related to #2376

See the test in the PR.

How do we type this behavior?

If its not possible (which I sorta expect but don't know for sure), then we should provide some type-cast option like:

// untested code ahead do not assume this is the solution :-) 

const _app = fastify()

const app = _app as unknown as () => Promise<FastifyInstance>

await app; // resolves FastifyInstance
@Ethan-Arrowood Ethan-Arrowood added typescript TypeScript related good first issue Good for newcomers labels Jul 7, 2020
@kbkk
Copy link

kbkk commented Jul 21, 2020

@Ethan-Arrowood Could you explain why wouldn't it work?

You can await anything in JS/TS, not just promises - it'll just return the awaited value.

So, given the above, this compiles/works just fine:

import * as fastify from "fastify";

const _app = fastify.fastify()

const app = await _app;

I also see no use for this

@mcollina
Copy link
Member

mcollina commented Jul 21, 2020

The fastify instance is a Thenable, it implements a then() method that will be called by await. This custom method will load all the plugins registered so far.

@Ethan-Arrowood
Copy link
Member Author

Ethan-Arrowood commented Jul 21, 2020

Understandable, but if I'm not mistake, this issue is specific to the promise behavior thenable aspect of the main fastify function. As a TS dev, if I await something I'm expecting that it's type is a promise as well

@fox1t
Copy link
Member

fox1t commented Jul 21, 2020

This is really a great point @Ethan-Arrowood and I somehow agree with you.

@L2jLiga
Copy link
Member

L2jLiga commented Jul 22, 2020

@Ethan-Arrowood correct me if I'm wrong but can't we use types union?

See #2428

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers typescript TypeScript related
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants