Latest version of hapi (`20.1.2`) or `@types/hapi__hapi 20.0.5` not compatible with latest `@hapi/podium` version (`4.1.2`)
Support plan
- is this issue currently blocking your project? (yes/no): yes
- is this issue affecting a production system? (yes/no): no
Context
- node version: 14
- module version with issue: 20.1.2 and older
- 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:
What are you trying to achieve or the steps to reproduce?
I do yarn upgrade whenever I work on a project, to be up-to-date with library versions. Today when I did that, the @hapi/podium version upgraded to 4.1.2 as inside package.json of the @hapi/hapi module there is "@hapi/podium": "^4.1.1" indicating to upgrade. But I get a typescript error as below. The types module @types/hapi__hapi has latest verion 20.0.5 only. Am not sure of issue in the code or just types.
What was the result you got?
node_modules/@types/hapi__hapi/index.d.ts:260:18 - error TS2430: Interface 'RequestEvents' incorrectly extends interface 'Podium'.
Types of property 'on' are incompatible.
Type '{ (criteria: "peek", listener: PeekListener): void; (criteria: "disconnect" | "finish", listener: (data: undefined) => void): void; }' is not assignable to type '<Tcontext extends object = this>(criteria: string | CriteriaObject, listener: Listener<Tcontext>, context?: Tcontext | undefined) => this'.
Types of parameters 'listener' and 'listener' are incompatible.
Types of parameters 'tags' and 'encoding' are incompatible.
Type 'string' is not assignable to type '{ [tag: string]: true | undefined; } | undefined'.
260 export interface RequestEvents extends Podium {
~~~~~~~~~~~~~
node_modules/@types/hapi__hapi/index.d.ts:633:18 - error TS2430: Interface 'ResponseEvents' incorrectly extends interface 'Podium'.
Types of property 'on' are incompatible.
Type '{ (criteria: "peek", listener: PeekListener): void; (criteria: "finish", listener: (data: undefined) => void): void; }' is not assignable to type '<Tcontext extends object = this>(criteria: string | CriteriaObject, listener: Listener<Tcontext>, context?: Tcontext | undefined) => this'.
Types of parameters 'listener' and 'listener' are incompatible.
Types of parameters 'tags' and 'encoding' are incompatible.
Type 'string' is not assignable to type '{ [tag: string]: true | undefined; } | undefined'.
633 export interface ResponseEvents extends Podium {
~~~~~~~~~~~~~~
node_modules/@types/hapi__hapi/index.d.ts:2363:43 - error TS2314: Generic type 'Listener' requires 1 type argument(s).
2363 removeListener(criteria: K, listener: Podium.Listener): this;
~~~~~~~~~~~~~~~
node_modules/@types/hapi__hapi/index.d.ts:2381:18 - error TS2430: Interface 'ServerEvents' incorrectly extends interface 'Podium'.
The types returned by 'on(...)' are incompatible between these types.
Type 'void' is not assignable to type 'this'.
'this' could be instantiated with an arbitrary type which could be unrelated to 'void'.
2381 export interface ServerEvents extends Podium {
~~~~~~~~~~~~
node_modules/@types/hapi__hapi/index.d.ts:2447:44 - error TS2314: Generic type 'Listener' requires 1 type argument(s).
2447 removeListener(name: string, listener: Podium.Listener): Podium;
What result did you expect?
tsc compile success.
We're also getting this with the new @hapi/podium release.
Here's the change in question: https://github.com/hapijs/podium/pull/67/files. In essence, an array was shallowly cloned. I expect that the fix will be to the types maintained on DefinitelyTyped, but if someone detects an issue with the code we can certainly revisit it. Here are podium's and hapi's types on there:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/hapi__podium/index.d.ts https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/hapi__hapi/index.d.ts
This will be from the new types in https://github.com/hapijs/podium/pull/63, which was not published until the recent bugfix. The types should probably have been published as a minor release (as per https://github.com/hapijs/podium/pull/63#issuecomment-703301733).
I expect the fix will reside in DT, which needs to handle the new official podium typings.
Or possibly revert https://github.com/hapijs/podium/pull/63 for now, and do a new patch release?
I can revert https://github.com/hapijs/podium/pull/63, but does it give us a clear path to releasing podium with those typings? I agree it should have gone out as a minor, but even if we do so I think folks will run into the same issue since it's a transitive dependency of hapi either way. I am happy to defer to typescript users on it 👍
I found an intermediate fix for the issue, that can be applied locally.
Just add a path override to the tsconfig.json file:
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@hapi/podium": [
"node_modules/@types/hapi__podium"
]
}
}
(If you're new to the thread, you may be interested in the workaround above 👆 https://github.com/hapijs/hapi/issues/4240#issuecomment-810860745)
Any typescripters around— how would you like to see things proceed? Our goal is to eventually land types inside the podium module. I see two paths:
- We rollback the podium types. The downside here is that it doesn't really define a clear path to releasing those types. No matter what version of podium is released (as a major, minor, or patch) in the future, it still may be installed in your project because it's a dependency of hapi itself. You should be able to control this somewhat using your package-lock, however, or use Gil's workaround above.
- A fix is contributed to the types for @hapi/hapi in DefinitelyTyped. This certainly makes our life easy on the hapi side! And I think one way or another this will need to happen. But I understand if now isn't the ideal time to worry about it.
Our inital workaround for production was to fork hapi, and change the version of podium in package.json. Then we switched to kanogil's solution. while I don't see it being an issue with people currently using hapi, it probably isn't the greatest initial user experience having hapi be broken if they pick it up.
That said, the latter path is definitely the path to take. While it's being done, you could just make the base hapi package point to 4.1.1
if you use yarn, you can add this to your package.json file to pin @hapi/[email protected]:
"resolutions": {
"@hapi/podium": "4.1.1"
}
and for any install after that, it will always resolve to that version.
My understanding is that a recent update to podium's types https://github.com/hapijs/podium/pull/70 will allow a contribution to hapi's typings on DefinitelyTyped bring things back in sync, so it seems like that is the next step to be taken here. If TypeScripters don't think that quite sounds right, feel free to chime-in. Following your lead! 👍
@SippieCup saw your suggestion, by the way! Happy to do that if there's some general consensus around it, but ideally as part of a broader plan to get the typings back in sync, as we wouldn't be able to leave it that way indefinitely.
@devinivy Definitely don't do what I suggested, that was just a quick hack to get things working in general.
You are correct in your approach of updating hapi's typings to get hapi back in partity.
If any community members would like to contribute the typing fixes to DefinitelyTyped, I believe that is still the next step to be taken here, and I'm sure hapi TypeScript users would much appreciate the contribution!
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/hapi__hapi/index.d.ts
TypeScript is now widely adopted for corporate projects. Many teams will not accept a package dependency without TypeScript support.
Maybe HapiJS could ship with TypeScript declarations, rather than relying on crowdsourced types from DefinitelyTyped?
The benefits:
- No puzzle of figuring out the right versions for
@types/hapi__hapi,@types/hapi__bell, etc. - The HapJS documentation could provide code samples for TypeScript developers
- Some TypeScript unit tests could be added, ensuring that new APIs don't ship without correct types
Part of the appeal of Fastify is that it treats TypeScript as a first class citizen.
Hi all. I was just trying to reproduce this, and I notice @types/hapi__hapi is now at 20.0.8 and includes the DefinitelyTyped PR https://github.com/DefinitelyTyped/DefinitelyTyped/pull/52509, which looks like it resolves this.
Is this still an open problem @rajeevnaikte ?
Oh great, thanks for the update! With https://github.com/DefinitelyTyped/DefinitelyTyped/pull/52509 released I expect that this is resolved, so I am closing. If it's not resolved just say the word and we'll re-evaluate. Thanks for all the help, everyone.