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

Fix #56: Better error messages for nullable types #96

Merged
merged 1 commit into from
Jun 29, 2021
Merged

Fix #56: Better error messages for nullable types #96

merged 1 commit into from
Jun 29, 2021

Conversation

oleg-slapdash
Copy link
Contributor

This fixes https://github.com/woutervh-/typescript-is/issues/56 by providing a better error message when a type is nullable. It only changes behaviour when a union contains 2 members (e.g. won't change behaviour for a | b | c) and when one of the union members is null null | a.

reateAssertType<{
  data: {
    node: { a: string; closed: string; b: string } | null;
  };
}>()({ data: { node: { a: "a", closed: false, b: "b" } } });

// Before:
// TypeGuardError: validation failed at $.data.node: there are no valid alternatives
// After: 
// TypeGuardError: validation failed at $.data.node.closed: expected a string, found: false

@oleg-slapdash oleg-slapdash marked this pull request as ready for review March 22, 2021 21:59
@DetachHead
Copy link
Contributor

does this only work on null? it should probably also do this for undefined / optional fields aas well

@oleg-slapdash
Copy link
Contributor Author

@DetachHead It works only with null, it does not work with Array<Type | undefined>, but it works with interface Type {optional?: OtherType | null}

@dko-slapdash
Copy link
Contributor

@woutervh- any plans on merging this PR? We’ve been using this patch for half a year so far (we keep a private fork of this module just in same of the single patch), it works well and is very useful in practice.

@dko-slapdash
Copy link
Contributor

@woutervh- - any plans to merge? We have to support our own fork at @slapdash/typescript-is for just this simple change, would be very convenient if it's merged directly into the official module.

@woutervh-
Copy link
Owner

Hi @dko-slapdash

Apologies for being late. Thanks for the reminder, I will merge and release this now :)

@woutervh- woutervh- merged commit 1527282 into woutervh-:master Jun 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants