Skip to content

Conversation

@benzaria
Copy link
Contributor

@benzaria benzaria commented Jun 5, 2025

  • ArrayReverse - Reverses the order of elements in a tuple or array type..

@benzaria benzaria marked this pull request as draft June 5, 2025 11:07
@benzaria benzaria closed this Jun 9, 2025
@benzaria benzaria deleted the Reverse branch June 9, 2025 12:16
@benzaria benzaria marked this pull request as ready for review June 21, 2025 13:48
@sindresorhus
Copy link
Owner

  • Export is missing. Add Reverse to index.d.ts
  • Needs to be added to readme
  • Should it be called Reverse or ReverseTuple?

Co-authored-by: Sindre Sorhus <[email protected]>
@benzaria
Copy link
Contributor Author

benzaria commented Sep 23, 2025

Hey @sindresorhus, it's been a while.
I will address the suggested changes and I think the name Reverse is fitting cuz its for arrays and tuples so using ReverseTuple will be misleading

Head extends UnknownArray = [],
Tail extends UnknownArray = [],
> =
keyof Array_ & `${number}` extends never // Is `Array_` leading a rest element or empty
Copy link
Owner

Choose a reason for hiding this comment

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

Would be useful with a short code comment that describes the algorithm.

// Edge cases
expectType<Reverse<[]>>([]);
expectType<Reverse<any>>(any);
expectType<Reverse<never>>(never);
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
expectType<Reverse<never>>(never);
expectType<Reverse<never>>(never);
expectType<Reverse<[unknown]>>([{} as unknown] as const);

@sindresorhus
Copy link
Owner

#1166 (comment) was merged. You mentioned you needed it for this.

// Optional/undefined
expectType<Reverse<[1?, 2?, 3?]>>([3, 2, 1] as const);
expectType<Reverse<[1?, 2?, 3?], {preserveOptionalModifier: true}>>({} as [3?, 2?, 1?]);
expectType<Reverse<[1, 2?, 3?], {preserveOptionalModifier: true}>>({} as [3 | undefined, 2 | undefined, 1]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Behaviour with optional elements is not correct, you can't return [3 | undefined, 2 | undefined, 1] for Reverse<[1, 2?, 3?]> because at runtime input could be [1, 2] and reversing it would produce [2, 1] which wouldn't conform to [3 | undefined, 2 | undefined, 1].

So, the output for cases containing optional elements needs fixing.

@som-sm som-sm mentioned this pull request Oct 11, 2025
@benzaria
Copy link
Contributor Author

@som-sm
Yeah, please go ahead, we can close this PR and move ahead with yours if that feels better.

I’ve managed to:

  • Incorporate the suggested change by adding TailAcc | to the _Reverse recursive call.
  • Fix the case where all elements are optional.
  • Remove ReverseOptions and refactor the implementation for clarity.
  • Update the JSDoc and tests to reflect the new behavior.

If there are any missing tests or additional scenarios worth covering, feel free to list or add them.

@som-sm
Copy link
Collaborator

som-sm commented Oct 12, 2025

Fix the case where all elements are optional.

@benzaria Please check #1266 (comment).

@benzaria
Copy link
Contributor Author

benzaria commented Oct 12, 2025

@benzaria Please check #1266 (comment).

@som-sm I took a note of that and I fixed the problem.

@category Array
*/
export type Reverse<Array_ extends UnknownArray> =
Copy link
Collaborator

@som-sm som-sm Oct 12, 2025

Choose a reason for hiding this comment

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

Better to name the type ArrayReverse as mentioned in #1266.

I've kept the name as ArrayReverse instead of just Reverse to match with our existing Array* types (like ArraySlice, ArraySplice).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Cases like labelled tuples, non-tuple arrays seem missing. Can you once cross-check from the tests in #1266.

@benzaria benzaria changed the title Add Reverse type Add ArrayReverse type Oct 12, 2025
@benzaria benzaria requested a review from som-sm October 12, 2025 22:21
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.

3 participants