Skip to content

.array().items() dynamic type item type schema causes issues in typescript #3086

@SimonSchick

Description

@SimonSchick

Runtime

node

Runtime version

24

Module version

18

Last module version without issue

17.x

Used with

hapi

Any other relevant information

It seems that a lot of expressions where the schema for array.items is dynamically chosen makes typescript really unhappy.

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

import joi from 'joi';

joi.array().items(Math.random() > 0.5 ? joi.string() : joi.number());

What was the result you got?

No overload matches this call.
  Overload 1 of 7, '(a: SchemaLikeWithoutArray<number>): ArraySchema<number[]>', gave the following error.
    Argument of type 'StringSchema<string> | NumberSchema<number>' is not assignable to parameter of type 'SchemaLikeWithoutArray<number>'.
      Type 'StringSchema<string>' is not assignable to type 'SchemaLikeWithoutArray<number>'.
        Type 'StringSchema<string>' is not assignable to type 'StringSchema<number>'.
          Type 'string' is not assignable to type 'number'.
  Overload 2 of 7, '(...types: SchemaLikeWithoutArray<number>[]): ArraySchema<any[]>', gave the following error.
    Argument of type 'StringSchema<string> | NumberSchema<number>' is not assignable to parameter of type 'SchemaLikeWithoutArray<number>'.
      Type 'StringSchema<string>' is not assignable to type 'SchemaLikeWithoutArray<number>'.
        Type 'StringSchema<string>' is not assignable to type 'StringSchema<number>'.
          Type 'string' is not assignable to type 'number'.ts(2769)

What result did you expect?

No error.

Metadata

Metadata

Assignees

Labels

bugBug or defecttypesTypeScript type definitions

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions