Skip to content

Possible typing regression between v21.3.12 and v21.4.4 #4561

@lgraziani2712

Description

@lgraziani2712

Runtime

node.js

Runtime version

24.11.1

Module version

21.4.4

Last module version without issue

21.3.12

Used with

hapi application

Any other relevant information

Is related to changes on TS typings

What are you trying to achieve or the steps to reproduce?

declare module '@hapi/hapi' {
    interface ReqRefDefaults {
        AuthCredentialsExtra: { some_id?: string }
    }
}

export function processAuth<Refs extends ReqRef>(req: Request<Refs>): void {
  if (
    !req.auth.user &&
    req.auth.isAuthenticated &&
    // Here, .some_id doesn't exists and will fail with ts2339 https://typescript.tv/errors/#ts2339
    req.auth.credentials.some_id
  ) {
    // Do something
  }
}

I need to use a generic here because this function is called in multiple routes where each of them has different specification for Params or Headers properties. For what I've seen, the typing changes introduced a "bug" (IDK if is actually a bug or a strict validation).

If instead of a generic, I define a real interface, the bug won't happen, but then I will get issues on each of the calls. E.g.

// file a.ts
// No more generic
export function processAuth(req: Request): void {
  if (
    !req.auth.user &&
    req.auth.isAuthenticated &&
    req.auth.credentials.some_id
  ) {
    // Do something
  }
}

// file b.ts
import { processAuth } from './a.js';

// define route and then in handler
server.route<{ Params: SomeParamInterface; }>({
  async handler(req, h) {
    // This will fail because local req and the defined param req have two different ReqRes interfaces.
    // This wasn't an issue with 21.3
    processAuth(req);
  }
})

What was the result you got?

A TS error on the line

    req.auth.credentials.some_id

What result did you expect?

To resolve the generics accordingly when merging the attributes between the Generic and the Default interfaces as v21.3.12 does.

Thank you in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugBug or defect

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions