Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly infer literal from enum #47

Open
rubengmurray opened this issue May 9, 2022 · 1 comment
Open

Correctly infer literal from enum #47

rubengmurray opened this issue May 9, 2022 · 1 comment

Comments

@rubengmurray
Copy link

It would be great to see support of enum values through to extracted types.

I've just tested out the following code with the @latest tag of @hapi/joi & joi-extract-type and it appears they aren't carried through.

Are there any plans to support? Has it been looked at before?

import * as joi from '@hapi/joi';
import 'joi-extract-type';

enum Options {
  OPTION_ONE = 1,
  OPTION_TWO = 2,
  OPTION_THREE = 3
};

const optionArgs = joi.object({
  option: joi.number().valid(...Object.values(Options).filter(o => typeof o === 'number'))
})

type OptionSchema = joi.extractType<typeof optionArgs>

// passes (expect to pass)
const myObjOne: OptionSchema = {
  option: 1
};

// passes (expect to fail)
const myObjTwo: OptionSchema = {
  option: 9
};

// fails (expect to fail)
const myObjThree: OptionSchema = {
  option: 'help'
};

Versions

  "dependencies": {
    "@hapi/joi": "17.1.1",
    "joi-extract-type": "15.0.8"
  }
@rubengmurray rubengmurray changed the title Infer strict subset from enum Correctly infer literal from enum May 9, 2022
@rubengmurray
Copy link
Author

Just occurred to me this is not necessarily enum specific and is more of a support of the valid() method...

Looks like it's mentioned here a while back 👀

#5 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant