-
-
Notifications
You must be signed in to change notification settings - Fork 893
doc: Fix JTD typescript sample error #1562
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
Conversation
const ajv = new Ajv() | ||
|
||
interface MyData = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is indeed a typo ...
docs/api.md
Outdated
foo: number | ||
bar?: string | ||
} | ||
|
||
const mySchema: JTDSchemaType<MyData> = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... but without explicitly defining schema type, the serializer would not check data type, as the comment below says - why is it removed here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I use typescript code below, tsc couldn't compile it.
import Ajv, {JTDSchemaType} from "ajv/dist/jtd"
const ajv = new Ajv()
interface MyData {
foo: number
bar?: string
}
const mySchema: JTDSchemaType<MyData> = {
properties: {
foo: {type: "int32"} // any JTD number type would be accepted here
},
optionalProperties: {
bar: {type: "string"}
}
}
const serializeMyData = ajv.compileSerializer(mySchema)
$ npx ts-node src/main.ts
⨯ Unable to compile TypeScript:
src/main.ts:11:11 - error TS2322: Type 'string' is not assignable to type 'never'.
11 foo: {type: "int32"} // any JTD number type would be accepted here
~~~~
src/main.ts:14:11 - error TS2322: Type 'string' is not assignable to type 'never'.
14 bar: {type: "string"}
~~~~
But when I use code below, tsc could compile it.
Plus I thought the result may be correct.
I'm not so confident since I am beginner here.
import Ajv, {JTDSchemaType} from "ajv/dist/jtd"
const ajv = new Ajv()
interface MyData {
foo: number
bar?: string
}
// const mySchema: JTDSchemaType<MyData> = {
const mySchema = {
properties: {
foo: {type: "int32"} // any JTD number type would be accepted here
},
optionalProperties: {
bar: {type: "string"}
}
}
const serializeMyData = ajv.compileSerializer(mySchema)
console.log(serializeMyData({"foo": "123", "baz": 123})); // => {"foo":123}
console.log(serializeMyData({"foo": "123"})); // => {"foo":123}
console.log(serializeMyData({})); // => {"foo":undefined}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There can be two reasons - typescript prior to 4.2.3 or not set strictNullChecks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the error message the latter seems more likely
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried unset strictNullChesk. But it didn't work.
tsconfig.json
{
"compilerOptions": {
"strictNullChecks": false
}
}
$ npx ts-node --project /path/to/tsconfig.json src/main.ts
⨯ Unable to compile TypeScript:
src/main.ts:12:11 - error TS2322: Type 'string' is not assignable to type 'never'.
12 foo: {type: "int32"} // any JTD number type would be accepted here
~~~~
src/main.ts:15:11 - error TS2322: Type 'string' is not assignable to type 'never'.
15 bar: {type: "string"}
~~~~
I tried to use older version of typescript. It didn't work either.
Since I can't understand why, should I revert the type change of mySchema ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strictNullChecks should be true - I will check the example a bit later
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typescript should be at least 4.2.3, not older
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also use it without schema type, and explicitly pass type parameter to compileSerializer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your help. It worked.
{
"compilerOptions": {
"strictNullChecks": true
}
}
import Ajv, {JTDSchemaType} from "ajv/dist/jtd"
const ajv = new Ajv()
interface MyData {
foo: number
bar?: string
}
const mySchema: JTDSchemaType<MyData> = {
// const mySchema = {
properties: {
foo: {type: "int32"} // any JTD number type would be accepted here
},
optionalProperties: {
bar: {type: "string"}
}
}
const serializeMyData = ajv.compileSerializer(mySchema)
console.log(serializeMyData({foo: 123, bar: "str"})); // => {"foo":123,"bar":"str"}
console.log(serializeMyData({foo: 123})); // => {"foo":123}
I'll fix the code change.
What issue does this pull request resolve?
JTD sample code for typescript does not work. tsc couldn't compile it.
ajv: 8.1.0
typescript: 4.2.4
What changes did you make?
After fixing it. I think that it work.
$ npx ts-node src/main.ts {"foo":123}
Is there anything that requires more attention while reviewing?