-
-
Notifications
You must be signed in to change notification settings - Fork 669
Expose ArrayLength type
#1344
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
base: main
Are you sure you want to change the base?
Expose ArrayLength type
#1344
Conversation
source/array-length.d.ts
Outdated
| /** | ||
| 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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
|
Strange, the IDE inferred |
som-sm
left a comment
There was a problem hiding this 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.
source/array-length.d.ts
Outdated
| /** | ||
| 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. |
There was a problem hiding this comment.
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.
|
@jericirenej Updated the implementation of export type ArrayLength<T extends readonly unknown[]> = T['length'];
type T = ArrayLength<any>
//=> anyThe previous implementation returned type ArrayLength<T extends readonly unknown[]> = T extends {readonly length: infer L} ? L : never;
type T = ArrayLength<any>
//=> unknownAlso, the updated implementation is much simpler. |
|
@som-sm Looks great, thanks! 👍 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, |
|
I think it's useful as documentation. It's easy to say that |
|
Ok, I'll update the base type description and docs later today! |
…e (determinable -> fixed)
Removed additional explanation for ArrayLength.
9245f0e to
6d4390d
Compare
|
Rebased to current Hopefully the added documentation is clear and concise enough: |
Issue #1341 proposed either deleting or exporting the unused
ArrayLengthinternal type.The issue was marked as a duplicate of #676 with the comment that exposing internal types is encouraged.
In this PR:
ArrayLengthis moved fromìnternal/array.d.tsto a separate file insource.