arb / celebrate

A joi validation middleware for Express.
MIT License
1.33k stars 66 forks source link

TypeScript Error: Type 'P' does not satisfy the constraint 'Params'. Type 'P' is not assignable to type 'ParamsArray'. #203

Closed nathan815 closed 3 years ago

nathan815 commented 3 years ago

My joi schema looks like this:

not applicable

The issue I am having with celebrate is:

I am getting this Typescript compilation error as soon as I import celebrate:

node_modules/celebrate/lib/index.d.ts(26,77): error TS2344: Type 'P' does not satisfy the constraint 'Params'.
  Type 'P' is not assignable to type 'ParamsArray'.
node_modules/celebrate/lib/index.d.ts(40,23): error TS2344: Type 'P' does not satisfy the constraint 'Params'.
  Type 'P' is not assignable to type 'ParamsArray'.
node_modules/celebrate/lib/index.d.ts(84,196): error TS2344: Type 'P' does not satisfy the constraint 'Params'.
  Type 'P' is not assignable to type 'ParamsArray'.
node_modules/celebrate/lib/index.d.ts(94,146): error TS2344: Type 'P' does not satisfy the constraint 'Params'.
  Type 'P' is not assignable to type 'ParamsArray'.

Not sure how exactly to fix this yet, but:

  1. Here is the TS declaration in celebrate/lib/index.d.ts:84:
    export declare function celebrate<P=ParamsDictionary, ResBody=any, ReqBody=any, ReqQuery=Query>(
    requestRules: SchemaOptions, joiOpts?: ValidationOptions, opts?: CelebrateOptions
    ): RequestHandler<P, ResBody, ReqBody, ReqQuery>;
                  ^
  2. These generic declarations were introduced in PR #200 about a month ago.
nathan815 commented 3 years ago

Mentioning @aarifkhamdi as author of #200.

nathan815 commented 3 years ago

Workaround: downgrade celebrate to version 13.0.3.

npm install celebrate@13.0.3 --save-exact
aarifkhamdi commented 3 years ago

how can i reproduce this error? try to provide repository maybe the parameter you are passing does not match the expected type

nathan815 commented 3 years ago

Hi @aarifkhamdi thanks for the response. I'm not passing any parameters; the error is happening in celebrate's declaration file on line 84 of lib/index.d.ts even without any code referencing celebrate.

Maybe it's a version issue? I am on express 4.17.1 (other versions are in first comment). The error goes away when I downgrade to celebrate 13.0.3 which is right before the generic types were introduced. I put a caret pointing at the type parameter causing the error in first comment.

I dug through the types here and could not find exactly what is causing this. RequestHandler should accept a ParamsDictionary (P) type just fine. I'm not even sure where the type ParamsArray is coming in to the picture.

I'll try making a public repo to reproduce this.

nathan815 commented 3 years ago

Looks like this problem was actually caused by @types/express. I was able to fix it by upgrading @types/express to 4.17.9 🙂

Closing this issue.

arb commented 3 years ago

Should the type defs for express be included in celebrate to prevent this? I'm not super familiar with how other packages handle their type def files and dependencies.

nathan815 commented 3 years ago

@arb I'm not sure if it would help since the application using celebrate still needs to have express and the express types package installed anyway. From what I read this has been a frequent issue with express and its types package.

arb commented 3 years ago

Right but I could move those type definition files into the formal dependencies of celebrate so they are installed along with celebrate? I don't personally use TypeScript so I don't really have any real world experience dealing with these kinds of issues or how they are typically solved by other modules.

Frankly, the type script definition file has been the most troublesome aspect of this module and I've contemplated removing them several times.