Skip to content

[TypeScript] Allow returning Reply from handlers #3923

@SimenB

Description

@SimenB

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

Currently, a handler is allowed to return void | Promise<RouteGeneric['Reply'] | void> , see

) => void | Promise<RouteGeneric['Reply'] | void>

This means that in an async handler, we can do this:

app.get('/', async (request, reply) => {
  const result = await someWork();

  if (result) {
    return reply.status(201).send();
  }

  return reply.send();
});

However, if we remove the async-await, this is now a type error. The solution is to move the return to its own line, but ideally returning the Reply object would be allowed both wrapped in a Promise and not.

(another workaround is return Promise.resolve(reply.status(201).send()) which is even weirder).


It seems to me this was considered in v4, but not acted upon (yet? 😀):

> extends infer Return ?
(void | Promise<Return | void>)
// review: support both async and sync return types
// (Promise<Return> | Return | Promise<void> | void)
: unknown

Motivation

Being able to return Reply from synchronous handlers makes for more concise code, and would be consistent with asynchronous handlers.

Example

See "Feature Proposal"

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions