Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type definition for the function any.when() mismatch the documentation and usage #2555

Closed
flowHater opened this issue Feb 3, 2021 · 1 comment
Labels
bug Bug or defect types TypeScript type definitions
Milestone

Comments

@flowHater
Copy link
Contributor

flowHater commented Feb 3, 2021

Support plan

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

Context

  • node version: 14.14
  • module version with issue: 17.1.1
  • last module version without issue: ?
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): Express
  • any other relevant information:

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

When I'm using the any.when() function with the shortcut for switch case:
From (https://joi.dev/api/?v=17.3.0#anywhencondition-options):

const schema = Joi.object({
    a: Joi.number().required(),
    b: Joi.number()
        .when('a', [
            { is: 0, then: 1 },
            { is: 1, then: 2 },
            { is: 2, then: 3, otherwise: 4 }
        ])
});

What was the result you got?

tsc throw me an error saying that the second param options any.when cannot be an [].

Overload 1 of 2, '(ref: string | Reference, options: WhenOptions): AlternativesSchema', gave the following error.
    Type '({ is: myKey; then: StringSchema; } | { is: myKey; then: StringSchema; otherwise: Schema; })[]' has no properties in common with type 'WhenOptions'.
  Overload 2 of 2, '(ref: Schema, options: WhenSchemaOptions): AlternativesSchema', gave the following error.
    Argument of type 'string' is not assignable to parameter of type 'Schema'.

What result did you expect?

Can transpile from TS to JS without any error.

I think I spoted the problem in the index.d.ts:
https://github.com/sideway/joi/blob/master/lib/index.d.ts#L1161
Or
https://github.com/sideway/joi/blob/master/lib/index.d.ts#L2184

@brianle1301
Copy link
Contributor

Feel free to submit a PR to resolve the issue.

@brianle1301 brianle1301 added the types TypeScript type definitions label Feb 3, 2021
@hueniverse hueniverse added the bug Bug or defect label Feb 7, 2021
@hueniverse hueniverse added this to the 17.3.1 milestone Feb 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug or defect types TypeScript type definitions
Projects
None yet
Development

No branches or pull requests

3 participants