Skip to content

Conversation

benzaria
Copy link
Contributor

@benzaria benzaria commented Jun 2, 2025

Closes #904

Added:

  • LiteralList a validator to check if a Tuple have exactly one of each member in given Union, No duplicate, Extras, or Less elements.
  • JoinUnion join a union members with the given delimiter (default: ',').

Example:

type Union = 'a' | 'b' | 'c' | 'd';

declare function func<const T extends unknown[]>(x: LiteralList<T, Union>): typeof x;
const bar1 = ['b', 'd', 'a', 'c'] as const
const bar2 = ['a', 'b', 'c', 'c'] as const
const bar3 = ['a', 'b', 'c'] as const

func(bar1); // Fine
func(bar2); // Error: Type ['a', 'b', 'c', 'c'] is missing Properties: ['d']
func(bar3); // Error: Type ['a', 'b', 'c'] is not the required Length of: 4

Errors example
Screenshot 2025-06-07 025631
Screenshot 2025-06-07 031342
Screenshot 2025-06-07 025742

 **Added**:
 - `LiteralList` a validator to check if a Tuple have exactly one of each member in given Union, No duplicate, Extras, or Less elements.
 - `JoinUnion` join a union members with the given delimiter (default: ',').

 **Modified**
 - `Join` delimiter (default: ','). Like `Array.join` default behavior.
Copy link
Collaborator

@som-sm som-sm left a comment

Choose a reason for hiding this comment

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

This PR doesn't look like it's ready.

Also, this needs further discussion about whether we want to do a type that returns error strings. And, this doesn't really close #904.

@benzaria
Copy link
Contributor Author

benzaria commented Jun 4, 2025

This PR doesn't look like it's ready.

Also, this needs further discussion about whether we want to do a type that returns error strings. And, this doesn't really close #904.

ur right this is a weird scenario to return strings as Errors, like they are not needed actually but they can add a visual reason why the type is erroring. we can remove them in case @sindresorhus refuses.

and lets be honest making the type #904 as suggested is not wort the effort for 4/5 union members

@benzaria
Copy link
Contributor Author

benzaria commented Jun 6, 2025

@sindresorhus Can u give a thought on returning the string Errors so we can go forward.

@sindresorhus
Copy link
Owner

I agree that returning Error is kinda weird, but since there is no other way to report errors to the users, I'm kinda willing to do it, because some weird Error is better than no indication of what's wrong.

I assume this solution does not suffer from performance issues like the one proposed in #904?

@som-sm
Copy link
Collaborator

som-sm commented Jun 6, 2025

Here's a quick demo of how it looks with a custom error string vs never.

Custom Error String Example

image

never Example

image

Surely feels better with the custom error string, just that we'd have to ensure the error string is built properly.

benzaria added 7 commits June 6, 2025 15:22
  - Fix: Corrected behavior when handling infinite-length lists (no longer returns true).
  - Add: Added comprehensive tests and documentation for `LiteralList`.
  - Improve: Enhanced error message formatting for clearer diagnostics.
@benzaria
Copy link
Contributor Author

benzaria commented Jun 7, 2025

@som-sm @sindresorhus I improved the errors! I think they look good and match with ts errors look
Screenshot 2025-06-07 025631
Screenshot 2025-06-07 031342
Screenshot 2025-06-07 025742

@benzaria benzaria requested a review from som-sm June 7, 2025 14:13
@benzaria
Copy link
Contributor Author

benzaria commented Jun 9, 2025

@som-sm PR is ready, can u review !

@benzaria benzaria requested a review from sindresorhus June 9, 2025 18:00
@sindresorhus
Copy link
Owner

Please run everything through AI, as requested multiple times on other pull requests (please do it on all your pull requests).

@benzaria benzaria requested a review from sindresorhus June 14, 2025 16:59
Copy link
Contributor Author

@benzaria benzaria left a comment

Choose a reason for hiding this comment

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

@sindresorhus Fixed Capitalized words in Error phrases

@benzaria benzaria requested a review from sindresorhus June 15, 2025 18:11
@benzaria
Copy link
Contributor Author

@som-sm Approved !

@som-sm
Copy link
Collaborator

som-sm commented Jun 18, 2025

Yeah, I'm aware. I'll get to this sometime this week.

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.

LiteralList<Union>
3 participants