Skip to content

Add LiteralList and JoinUnion types#1159

Open
benzaria wants to merge 31 commits intosindresorhus:mainfrom
benzaria:LiteralList
Open

Add LiteralList and JoinUnion types#1159
benzaria wants to merge 31 commits intosindresorhus:mainfrom
benzaria:LiteralList

Conversation

@benzaria
Copy link
Copy Markdown
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

Loading
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