Skip to content

Typescript: broken register with plugin array #4448

Open

Description

@Eomm

Support plan

  • is this issue currently blocking your project? (yes/no): no
  • is this issue affecting a production system? (yes/no): yes

Context

  • node version: 18.x
  • module version with issue: 21.3.1
  • last module version without issue: -
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): standalone
  • any other relevant information: typescript 4.9.5

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

By using server.register(pluginArray), typescript does not work.
Registering the same plugins one by one, it works.

import { Server, Plugin } from '@hapi/hapi';

interface TestPluginOptions {
  x: number;
}

interface TestPluginDecorations {
  plugins: {
    test: {
      add(a: number, b: number): number;
    };
  }
}

const plugin1: Plugin<TestPluginOptions, TestPluginDecorations> = {
  name: 'test',
  version: '1.0.0',
  register: function (srv, options) { }
};

const plugin2: Plugin<{}> = {
  name: 'tes2',
  version: '1.0.0',
  register: function (srvs, options) { }
};

const plugin3: Plugin<void> = {
  name: 'tes2',
  version: '1.0.0',
  register: function (srvs, options) { }
};

async function test() {
  const server = new Server();

  // does not work
  await server.register([
    { plugin: plugin1, options: { x: 1 } },
    { plugin: plugin2, options: {} },
    { plugin: plugin3, options: undefined },
  ]);

  // works
  await server.register({ plugin: plugin1, options: { x: 1 } });
  await server.register({ plugin: plugin2, options: {} });
  await server.register({ plugin: plugin3, options: undefined });
}

test()

What was the result you got?

Type '{ x: number; }' is not assignable to type 'void'.

Error:

Full stack

What result did you expect?

It should work with tsc index.ts

Activity

  1. added
    supportQuestions, discussions, and general support
    on Apr 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    supportQuestions, discussions, and general support

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions