Closed jasonaibrahim closed 2 years ago
It is ready to review now!
Hello @jasonaibrahim I tested the changes over the weekend but they don't seem to work on my env. And actually, it makes sense that it doesn't because we are not generating the type annotations from the Joi factory methods.
The expected behavior is to have types while mouseover variables Eg. on VSCode:
Let me know if it actually works for you. The good news is that I think I was able to augment the current Joi interface using the same version that you used. So I'll try to push a branch with what I have for joi@17
Nice - I would love to see how you approached it. I was not able to augment the sideway/joi
interface and ended up flattening and exporting all of the types instead, and unfortunately forfeited intellisense in the process
Hey @jasonaibrahim take a look: https://github.com/jasonaibrahim/joi-extract-type/pull/1 The gotcha there was to remove it from being included on the .tsconfig
I don't know if it will be possible to be integrated into another project because of that. When I include it says that I'm using "private names" or something like that from the module. But that's easier to solve by tweaking the Joi native types I think...
If you could test it, that would be great. I'm not running projects with Joi currently
any updates?
I wasn't able to get intellisense to work with this PR. It will run and compile, but you wouldnt get any type hints which is pretty much the point of this library. i would recommend using zod or yup if you have the capacity to migrate away from joi
I see, thanks for the tip! Based on those libs, I built my own small utility lib for ajv (similar to suretype) - I think it's quite cool:
const testObj = {
test: gReq(gStr({minLength: 2, maxLength: 10})),
tt: gOpt(gStrEnum(["a", "b"] as const)),
c: gReq(gStrConst("wer")),
a: gReq({
test: gReq(gStrConst("asd"))
}),
d: gReq(gDiscriminator([
{
a: gReq(gStrConst("a")),
b: gReq(gStr())
},
{
a: gReq(gStrConst("c"))
}
] as const, "a"))
}
type testObjType = inferType<typeof testObj>
const ajvSchema = gSchema(testObj, "test")
export function foo(t: testObjType) {
if (t.d.a == "a") {
// Intellisense checks that we can now access b
console.log(t.d.b)
}
}
Maybe I publish this utility at some point.
awesome! you should consider publishing it. im sure it would benefit many folks. ill close this pr for now.
So there is no plan to bring this feature to sideway/joi
?
Does this mean that there is currently no option to infer the type from a Joi schema? 😞
I wasn't able to get intellisense to work with this PR. It will run and compile, but you wouldnt get any type hints which is pretty much the point of this library.
Wouldn't it be at least something to be able to use the types for annotation and for compiling?
i'm a bit foggy on the reasons why it didn't work, but I can revisit again. if I manage to come up with a solution, I'll reopen this PR
My repo uses workspaces, I was validating the return type of apis. I noticed that After compiling the intellisense types were MUCH cleaner to look at, So I moved out all my validations to a workspace, and have it compile upon running install.
It should work for you as there definitely wouldn't be any intellisense issues for you when loading static .d.ts files.
This PR addresses #44 by dropping support for the deprecated @hapi/joi library and providing support for sideway/joi instead. This is accomplished by providing a named module and setting it as the default export. Type signatures that have changed in sideway/joi have also been patched in this PR.
The new way to use this library would be:
Create the schemas and use
extractType
to infer the type: