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

Require at least one element in arrays or function arguments (RequireAtLeastOne) #420

Closed
fregante opened this issue Jul 8, 2022 · 2 comments · Fixed by #915
Closed

Require at least one element in arrays or function arguments (RequireAtLeastOne) #420

fregante opened this issue Jul 8, 2022 · 2 comments · Fixed by #915

Comments

@fregante
Copy link
Collaborator

fregante commented Jul 8, 2022

I'm opening this issue for documentation/googlability, but it should also probably be added to https://github.com/sindresorhus/type-fest/blob/main/source/require-at-least-one.d.ts

It turns out, RequireAtLeastOne also works with arrays:

type NonEmptyArray = RequireAtLeastOne<unknown[], 0>;
export const full: NonEmptyArray = [2];
export const empty: NonEmptyArray = [];
// Type '[]' is not assignable to type 'NonEmptyArray'.
//   Property '0' is missing in type '[]' but required in type 'Required<Pick<unknown[], 0>>'.

It also works for functions:

export function atLeastOneArg(...numbers: RequireAtLeastOne<number[], 0>): number[] {
	return numbers;
}

atLeastOneArg(0);
atLeastOneArg();
// Argument of type '[]' is not assignable to parameter of type 'RequireAtLeastOne<number[], 0>'.
//  Property '0' is missing in type '[]' but required in type 'Required<Pick<number[], 0>>'.ts(2345)

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
fregante added a commit to refined-github/refined-github that referenced this issue Jul 8, 2022
@adam-rocska
Copy link

Nice idea, but won't cover for cases like 😢

const wontWork: RequireAtLeastOne<number[], 0> = [...[], ...[1]];

@fregante
Copy link
Collaborator Author

fregante commented Sep 9, 2023

As mentioned in #476, that's likely a non-resolvable TypeScript limitation (because it doesn't resolve the value/type deeply enough)

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 a pull request may close this issue.

2 participants