TCMiranda/joi-extract-type

AlternativeSchema interpreted as 'never'

Closed this issue ยท 4 comments

const schema = Joi.object({
  type: Joi.string().required(),
  foo: Joi.alternatives().when('type', {
      is: 'something',
      then: Joi.array()
        .items(Joi.string())
        .required(),
      otherwise: Joi.forbidden()
    })
});
type Thing = Joi.extractType<typeof schema>;
const thing: Thing = {
   type: 'something',
   foo: [] // error, foo: never cannot be assigned to any[]
}

This is a really kick-ass library, I've become very reliant on it since we don't have to duplicate our types + schemas with it! Thank you!

Hey @omairvaiyani nice to hear that!
So, I didn't work on alternatives().when. At first It would be easy to go with #2 extracting the type as A | B.
Ill try to work on it soon to implement a basic support.

That's great, good luck. I'd love to submit a PR, but my internal typescript knowledge is primitive unfortunately.

Actually, I've improved a lot since I started this project ๐Ÿ˜
The basic support is published @0.1.5

That was quick, thank you!