Skip to content

Fix: Tail with optional, readonly and non-tuple arrays#424

Merged
Beraliv merged 4 commits into
ts-essentials:masterfrom
som-sm:fix/tail-type-with-optional-and-readonly-arrays
Dec 5, 2024
Merged

Fix: Tail with optional, readonly and non-tuple arrays#424
Beraliv merged 4 commits into
ts-essentials:masterfrom
som-sm:fix/tail-type-with-optional-and-readonly-arrays

Conversation

@som-sm

@som-sm som-sm commented Dec 2, 2024

Copy link
Copy Markdown
Contributor

PR Checklist

  • Addresses an existing open issue: related to #000
  • Steps in Contributing were taken

Overview

I came across this issue in type-fest regarding their implementation of Tail, which doesn't work as expected when instantiated with an array of all optional elements. Our implementation of Tail also fails in this case; when instantiated with [1?, 2?, 3?], it returns never instead of the expected [2?, 3?].


Additionally, there are two other issues with the current implementation:

  1. Readonly Arrays: Tail incorrectly returns never when instantiated with readonly arrays.
  2. Non-Tuple Arrays: When applied to non-tuple arrays (e.g., string[]), Tail currently returns never. I believe it should instead return back the same type, e.g., Tail<string[]> should return string[]. However, in this scenario, type-fest also returns an empty array, which is kinda equivalent to our version of never, though I'm unsure why.

@Beraliv If you think we should continue to return never for non-tuple arrays, then the solution I proposed here should work.

@Beraliv

Beraliv commented Dec 2, 2024

Copy link
Copy Markdown
Collaborator

Hey @som-sm! I'll come back to you later today, apologies for late responses

Comment thread test/index.ts Outdated

@Beraliv Beraliv left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM!

Comment thread lib/tail/index.ts
@som-sm som-sm force-pushed the fix/tail-type-with-optional-and-readonly-arrays branch from b1213c5 to f32ecf8 Compare December 3, 2024 18:35
@Beraliv

Beraliv commented Dec 3, 2024

Copy link
Copy Markdown
Collaborator

@som-sm please finalise your changes and I'll be happy to get it merged

Comment thread lib/tail/index.ts
@som-sm

som-sm commented Dec 4, 2024

Copy link
Copy Markdown
Contributor Author

@Beraliv PR is finalised now, can be merged!

I'll see if Head requires any similar changes, will create a PR accordingly.

@Beraliv Beraliv merged commit dda4def into ts-essentials:master Dec 5, 2024
@Beraliv Beraliv added the v10 label Dec 5, 2024
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.

2 participants