TCMiranda / joi-extract-type

Provides native type extraction from Joi schemas for Typescript
MIT License
151 stars 27 forks source link

AlternativeSchema interpreted as 'never' #14

Closed omairvaiyani closed 5 years ago

omairvaiyani commented 5 years ago
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!

TCMiranda commented 5 years ago

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.

omairvaiyani commented 5 years ago

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

TCMiranda commented 5 years ago

Actually, I've improved a lot since I started this project :grin: The basic support is published @0.1.5

omairvaiyani commented 5 years ago

That was quick, thank you!