Skip to content

Conversation

@jericirenej
Copy link

Issue #1341 proposed either deleting or exporting the unused ArrayLength internal type.

The issue was marked as a duplicate of #676 with the comment that exposing internal types is encouraged.

In this PR:

  • ArrayLength is moved from ìnternal/array.d.ts to a separate file in source.
  • General tests added.
  • Type documentation has been expanded.

@jericirenej jericirenej changed the title feat/expose-array-length: Export ArrayLength. Add documentation and t… Expose `ArrayLength˙ type Jan 30, 2026
@jericirenej jericirenej changed the title Expose `ArrayLength˙ type Expose ArrayLength type Jan 30, 2026
/**
Infers the length of an array. Tuples resolve to numeric literals, while non-tuples resolve to the `number` type.

Useful for enforcing fixed-length arrays and distinguishing between tuple and non-tuple like arrays.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Useful for enforcing fixed-length arrays

What do you mean by this? Can you also add an example for this.

distinguishing between tuple and non-tuple like arrays

I suggest removing this because we already have the IsTuple type, which handles this case better. For example, IsTuple allows [number, ...string[]] to be treated as both a tuple and a non-tuple, whereas ArrayLength can only treat it as a non-tuple.

Copy link
Author

@jericirenej jericirenej Feb 2, 2026

Choose a reason for hiding this comment

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

Yes, it's a bit vague, I agree!
For enforcing fixed-length array this meant that it could have been used as part of a combined type that would be checking against some other numeric literal condition. For example:

type IsLength<
  Arr extends readonly unknown[],
  Num extends number,
> = IsEqual<ArrayLength<Arr>, Num>;

But this might all be a bit too contrived. And I agree with your point about the tuple / non-tuple distinction - the IsTuple helper does a much better job.

I'm fine with removing the "usefulness" sentence if it is not needed. I added it to replicate the documentation pattern observed in some other helper types. But the simplicity of the current one might make it hard to do so.

p.s.: I was resolving the conversations /comments as I was applying the changes - I should have left it to you to resolve it, since you made the actual change requests. Sorry about that, I was on auto-pilot 😄 Please feel free to unresolve any of them if I made an error somewhere.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you can remove this entirely. Adding contrived use cases doesn't really help, and it's a pretty simple type, so it doesn't necessarily need a use case section.

Copy link
Author

Choose a reason for hiding this comment

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

Agreed. I'll remove and push

@jericirenej
Copy link
Author

Strange, the IDE inferred unknown when specifying ArrayLength<any>, but tests failed, because there it was inferred as an any type. Updated.

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.

LGTM! Just few minor comments.

/**
Infers the length of an array. Tuples resolve to numeric literals, while non-tuples resolve to the `number` type.

Useful for enforcing fixed-length arrays and distinguishing between tuple and non-tuple like arrays.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you can remove this entirely. Adding contrived use cases doesn't really help, and it's a pretty simple type, so it doesn't necessarily need a use case section.

@sindresorhus
Copy link
Owner

@som-sm
Copy link
Collaborator

som-sm commented Feb 6, 2026

@jericirenej Updated the implementation of ArrayLength to the following:

export type ArrayLength<T extends readonly unknown[]> = T['length'];

type T = ArrayLength<any>
//=> any

The previous implementation returned unknown when instantiated with any. IMO, returning any is better.

type ArrayLength<T extends readonly unknown[]> = T extends {readonly length: infer L} ? L : never;

type T = ArrayLength<any>
//=> unknown

Also, the updated implementation is much simpler.

@jericirenej
Copy link
Author

jericirenej commented Feb 6, 2026

@som-sm Looks great, thanks! 👍
I was also curious initially why the original type was inferring the length instead of just accessing length directly. But at that point I did not venture further and just focused on providing some tests and documentation which would satisfy the requirements for the type to be exported.

This possibility of accessing the length directly was also one of the reasons that - when faced with the options to expose the type or remove it - I was leaning more into arguing for its omission in my initial post.

@som-sm
Copy link
Collaborator

som-sm commented Feb 7, 2026

This possibility of accessing the length directly was also one of the reasons that - when faced with the options to expose the type or remove it - I was leaning more into arguing for its omission in my initial post.

I agree, this type isn’t that useful. It’s essentially just syntactic sugar, T["length"] vs ArrayLength<T>. I’m also fine with removing it. @sindresorhus, WDYT?

@sindresorhus
Copy link
Owner

I think it's useful as documentation. It's easy to say that T["length"] is better, but most don't even know about it. I also find ArrayLength slightly more readable at callsite. So our docs can make it clar that T["length"] is also a good alternative.

@jericirenej
Copy link
Author

Ok, I'll update the base type description and docs later today!

@jericirenej jericirenej force-pushed the feat/expose-array-length branch from 9245f0e to 6d4390d Compare February 10, 2026 16:51
@jericirenej
Copy link
Author

jericirenej commented Feb 10, 2026

Rebased to current main and updated documentation.

Hopefully the added documentation is clear and concise enough:
Return the length of an array. Equivalent to `T['length']` where `T` extends any array.

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