Question: is `strictNullChecks` required to use ts-json-validator?
silasdavis opened this issue · 3 comments
Thank you for this project - I really like the approach.
I found that I could not get a fairly trivial example to compile in my project:
const parser = new TsjsonParser(
S({
type: 'object',
properties: {
payload: S({ type: 'string' }),
},
}),
);
function foo(v: any) {
if (parser.validates(v)) {
// error TS2532: Object is possibly 'undefined'.
console.log(v.payload);
}
}
The use of v
in v.payload
produces the error: TS2532: Object is possibly 'undefined'.
. If I check v !== undefined
to narrow the type of v
then payload
is inferred as never
.
After embedding this example in your tests (where it does work). It seems like the important piece of difference is that you have strictNullChecks
enabled via strict
in tsconfig.json
.
I assume that your type magic must rely on inference that needs there to not be implicit void types. I just wanted to check I am not missing something and this is in fact required since I did not see it mentioned anywhere. Is there a way round this? Perhaps I am mistaken to isolate it the issue to strictNullChecks
(I can do a more careful isolation if needed).
As a side-note I would like to enable strict
mode in all my projects, but I want to use this library in a large system of projects that I am incrementally typing so it's not something I can easily turn on without refactoring large amounts of code.
I haven't investigated yet but I wouldn't be surprised if this library breaks without strictNullChecks. I'll investigate ASAP and will either find a fix or add a disclaimer in the README.
Thanks for the report!
Let me know if I can help with this. Worth my while if it can be made to work.
I looked into this a bit, and it seems like it will be really tricky to get this working while keeping the library useful. I'm going to leave this open for a few more days in case I have an epiphany, but I don't think we're going to be able to get this working properly without --strictNullChecks.