-
-
Notifications
You must be signed in to change notification settings - Fork 868
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
[docs] relation between JTDSchemaType and JTDDataType #1617
Comments
I am not sure what is the intention, you might be using it incorrectly. It is not clear how the second sample relates to the first. What is loginReq, for example? Also, I don't think you can compare types with |
@epoberezkin Sorry, I was copying from my actual code and didn't finish renaming for the example. |
I think I understand now... What's the version of typescript? It is only supported with >4.2.3 |
I don't have my project handy at the moment, but unless a new version of typescript has come out in the last few days, I'm using the latest stable version. |
Yep, [email protected] |
Now I'm thinking it has to be something with my setup. type RES = {
ok: 1;
err: string[];
data: string;
} | {
ok: 0;
err: string[];
};
export const res: JTDSchemaType<RES> = {
discriminator: 'ok', // Type 'string' is not assignable to type 'never'
mapping: {
'0': {
properties: {
err: {elements:{type: 'string'}},
},
},
'1': {
properties: {
err: {elements:{type: 'string'}},
data: {type: 'string'},
},
},
},
}; Here's my tsconfig if it helps: {
"compilerOptions": {
"outDir": "./build",
"module": "es2020",
"target": "es2020",
"allowJs": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"checkJs": false,
"moduleResolution": "node",
"baseUrl": ".",
"lib": [
"ES2020"
],
"paths": {
"#models/*": [
"./models/*.js"
],
"#STypes/*": [
"./types/*.js"
],
"##": [
"./"
]
}
},
"exclude": [
"build",
"dist",
"node_modules"
]
} |
It should be |
Although maybe strict does something else? But you definitely need strictNullChecks for it to work. |
Having "strict": true,
/* Enable all strict type checking options.
See more: https://www.typescriptlang.org/tsconfig#strict */ |
Just tried explicitly enabling, still having the same issues. |
ah - I think for JTDDataType to work schema must be defined "as const", otherwise types are too generic for it to construct data type. |
but it does not seem to help... |
Yep that was partly it. I was trying to use JTDDataType to test the returned type of JTDSchemaType. |
Correct - just tested it - they are not "reversible". If you just pass the object "as const" you can derive JTDDataType from its type. But not from the type constructed by JTDSchemaType - it's a different more complex type that what |
You could test some |
I think most of this came from messing with it trying to find a way to have typed wrappers for my schemas. |
Thanks for working through this mostly without bugging me, but feel free to tag me earlier @epoberezkin. There's a few things to address. First, for discriminated unions, JTD spec only allows strings. If you change your "ok" numbers to strings, the schema checks out. This isn't normal javascript nonsense where types get coerced all the time. Second, there is a Lines 8 to 41 in be07d3d
Finally, and unfortunately, there can't be a nice correspondence between |
Do you know if there's a way to use a conditional type to only apply the SomeJTDSchemaType if it's invalid? export const getREQ_WRAPPER = <T>(
v: SomeJTDSchemaType extends T ? T : SomeJTDSchemaType,
) => ({
properties:{
data: v,
},
} as const); But after trying every combination I can't seem to get that to work. |
I'm not sure exactly what you're asking for, can you break it down a little bit to say exactly what you're writing, and where you want it to error? Unfortunately typescript doesn't have a way to throw an error with a conditional type (yet). Your two real options are to assign it to a string literal, or assign it to never, which will only throw once you try to set some value to it. If there problem is with the output of JTDDataType, then maybe you want to assign datatypes with |
As a side effect of wrapping my schemas I was hoping I could also use an enforced argument constraint to only apply const x = getREQ_WRAPPER({
text:{type:'string'}
} as const)
/*
typeof x is
{
readonly properties: {
readonly data: {
readonly text: {
readonly type: "string";
};
};
};
}
*/
const y = getREQ_WRAPPER({
invalid: 'hi',
/* Argument of type '{ readonly invalid: "hi"; readonly text: { readonly type: "string"; }; }' is not assignable to parameter of type 'SomeJTDSchemaType'.
Object literal may only specify known properties, and 'invalid' does not exist in type 'SomeJTDSchemaType'. */
text:{type:'string'}
} as const)
/*
typeof y is
{
readonly properties: {
readonly data: SomeJTDSchemaType;
};
}
*/ |
There are a few solutions that I think achieve what you're trying to do. The most trypscript way would be to just have compile / validate only take valid schemas. Unfortunately ajv is pretty permissive from a typescript font making this hard to work out. However you could define your own wrapper like:
You could actually return the validator interface, but that's the gist. Note, all you're doing is restricting the actual function overloads: Lines 359 to 362 in be07d3d
alternatively, you could just create a dummy checker variable:
will error when trying to assign to _, this is the close to a type assertion in typescript as it exists right now. Finally, if you still want your
and that will preserve the type while also verifying that it's |
@epoberezkin you updated this to a docs issue. Which aspect of this do you think needs to be documented? I'm happy to submit the PR just not sure what you think should be included. |
@erikbrinkman I don't think it needs to be explicitly mentioned in the docs tbh - up to you. I usually mark "docs" the issues that are themselves offer some useful information - whether it should be in the docs is the question of how many people need this info... |
@josh-hemphill did one of those solutions work? Is this resolved? |
closing due to inactivity, but feel free to reopen if this isn't resolved @josh-hemphill |
What version of Ajv are you using?
8.4.0
Does the issue happen if you use the latest version? YesJTD Schema
Your code
What results did you expect?
Are you going to resolve the issue?
If this is a bug, and I know where to start, sure.
The text was updated successfully, but these errors were encountered: