hapijs / joi

The most powerful data validation library for JS
Other
20.95k stars 1.51k forks source link

Types of `alternative` are wrong always resolves to any #2980

Closed felixmosh closed 1 year ago

felixmosh commented 1 year ago

Support plan

Context

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

const hostSchema = Joi.alternatives([Joi.string().ip(), Joi.string().domain().allow('localhost')]).required();

What was the result you got?

hostSchema gets a type of Joi.AlternativesSchema<any> instead of specific union of types, image

What result did you expect?

It should contain reflect the union type of the alternatives...

Maybe we can use the types from https://github.com/maghis/types-joi/blob/master/index.d.ts which do work

The problem is that SchemaLike is not related to the TSchema, In addition, Typescript doesn't supports rest params of different types, therefore, we probably need to do what is done here