Skip to content

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

@pierrick-martin-dev

Description

@pierrick-martin-dev

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugBug or defecttypesTypeScript type definitions

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions