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

Make literals work with null #698

Merged
merged 3 commits into from
Jul 11, 2023
Merged

Conversation

KristjanTammekivi
Copy link
Contributor

Closes #685

@samchon samchon self-assigned this Jul 2, 2023
@samchon samchon added invalid This doesn't seem right question Further information is requested labels Jul 2, 2023
Copy link
Owner

@samchon samchon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I have said, null is not a value categorized in atomic, but a type of object.

Therefore, this PR and related issue need to consider about that.

#685 (comment)

@KristjanTammekivi
Copy link
Contributor Author

Can you explain why that matters in the context of this issue/pr?

@samchon
Copy link
Owner

samchon commented Jul 11, 2023

I repeat #685 issue's comment.

typia.literals() function has been designed to get list of constant literal values.

  • boolean
  • number
  • bigint
  • string

The null value what you want is categorized in object type.` It's the problem.

@KristjanTammekivi
Copy link
Contributor Author

No problem then, null is a literal
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/null#description

The value null is written with a literal: null

Also typeof null is considered a bug that was never fixed https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof#typeof_null

And even if it wasn't a literal I would say that that it's an unnecessary self-imposed constraint. I don't think undefined is a literal (although typeof undefined === 'undefined' so going by that standard it might be), but I would argue that literals should work with that as well. Let me talk about my use case.

We use Joi for validation and want to provide a list of valid values for it. Because literals doesn't support null we have to instead do something like the following

type ValidValues = 'A' | 'B' | null;
const validValues = [...literals<Exclude<ValidValues, null>>(), null]

And this is why I want null to be included

@samchon
Copy link
Owner

samchon commented Jul 11, 2023

Wow, the null value to be considered an object type was a bug.

Got it. Your PR would be published as a next version v4.0.5.

Thanks for contribution.

@samchon samchon merged commit 2238479 into samchon:master Jul 11, 2023
samchon added a commit that referenced this pull request Jul 11, 2023
@samchon samchon added enhancement New feature or request and removed invalid This doesn't seem right labels Jul 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

Successfully merging this pull request may close these issues.

literals should accept null
2 participants