Skip to content
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

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

Open
silasdavis opened this issue May 23, 2020 · 3 comments
Open
Assignees
Labels
question Further information is requested

Comments

@silasdavis
Copy link

silasdavis commented May 23, 2020

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.

@ostrowr
Copy link
Owner

ostrowr commented May 26, 2020

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!

@ostrowr ostrowr self-assigned this May 26, 2020
@ostrowr ostrowr added the question Further information is requested label May 26, 2020
@silasdavis
Copy link
Author

Let me know if I can help with this. Worth my while if it can be made to work.

@ostrowr
Copy link
Owner

ostrowr commented Jun 7, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants