Skip to content

Question: is strictNullChecks required to use ts-json-validator? #34

@silasdavis

Description

@silasdavis

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.

Metadata

Metadata

Assignees

Labels

questionFurther information is requested

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions