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

fix: add the type readonly array to the partition #32

Merged
merged 7 commits into from
Jun 13, 2024

Conversation

ssi02014
Copy link
Contributor

@ssi02014 ssi02014 commented Jun 11, 2024

Hello 👋 Here's a suggestion.
Currently, it does not work correctly for arrays with const assertion applied.

I think it would be nice to add a readonly T[] type, what do you think?

AS-IS

스크린샷 2024-06-11 오후 10 34 51
function partition<unknown>(
  arr: unknown[],
  isInTruthy: (value: unknown) => boolean
): [truthy: unknown[], falsy: unknown[]];
스크린샷 2024-06-11 오후 10 35 30
function partition<unknown>(
  arr: unknown[],
  isInTruthy: (value: unknown) => boolean
): [truthy: unknown[], falsy: unknown[]];

TO-BE

const arr = [1, 2, 3, 4, 5] as const;

const [evnes, odds] = partition(arr, (item) => item % 2 === 0);
// const evens: (1 | 2 | 3 | 4 | 5)[]
// const odds: (1 | 2 | 3 | 4 | 5)[]
const arr = [
  { name: "mj", age: 27 },
  { name: "gromit", age: 30 },
] as const;

const [ob, yb] = partition(arr, (item) => item.age >= 30);
/*
  const ob: ({
    readonly name: "mj";
    readonly age: 27;
  } | {
    readonly name: "gromit";
    readonly age: 30;
  })[]

 const yb ({
    readonly name: "mj";
    readonly age: 27;
  } | {
    readonly name: "gromit";
    readonly age: 30;
  })[]
*/

Copy link

vercel bot commented Jun 11, 2024

@ssi02014 is attempting to deploy a commit to the Toss Team on Vercel.

A member of the Team first needs to authorize it.

@ssi02014
Copy link
Contributor Author

I think this readonly T[] type should be added to other array utility functions as well.
I can work on it incrementally if it makes sense and is needed. 🤗

@ssi02014 ssi02014 changed the title fix: Add the type readonly array to the partition fix: add the type readonly array to the partition Jun 11, 2024
@raon0211
Copy link
Collaborator

raon0211 commented Jun 13, 2024

Hi @ssi02014, thanks for your contribution!

As you mentioned, our library does not currently support readonly arrays.

Meanwhile, This would affect a large portion of our function interfaces, and I think using readonly types in TypeScript is a minor use case.

Because of our design principles which value simplicity, I think this would not meet our design principle.

What do you think?

@ssi02014
Copy link
Contributor Author

ssi02014 commented Jun 13, 2024

@raon0211 Thank you for your feedback 🙏

In my case, I wanted to narrow down the types as much as possible in my development, so I was disappointed to see a type error when using a readonly array.

Because of our design principles which value simplicity, I think this would not meet our design principle.

The readonly array can be used in more places than you might think, and I didn't think adding that type broke the principle of simplicity, but that might be a matter of perspective.

I think it's worth considering whether it's more appropriate to keep the existing principle and preclude users from using readonly, or to add the readonly type but change the function interface.

@codecov-commenter
Copy link

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

@raon0211
Copy link
Collaborator

raon0211 commented Jun 13, 2024

How about this?

Since T[] can be assigned to readonly T[], we can change all our array utilities to accept only readonly T[].

For example, the signature of partition would become:

function partition<T>(arr: readonly T[], isInTruthy: (value: T) => boolean): [truthy: T[], falsy: T[]];

However, I'd like our documentation to state that partition accepts T[] as its argument (not readonly). This is because many of our users might get confused if they see that partition only accepts readonly arrays.

@ssi02014
Copy link
Contributor Author

ssi02014 commented Jun 13, 2024

@raon0211
Yes. I like your suggestion as well, as it fulfills my initial purpose of "readonly array are compatible".

But, How do you feel about documentation and code being out of sync, which can lead to confusion?

I will follow the direction of the es-toolkit maintainers! 🤗

@raon0211
Copy link
Collaborator

I think for the sake of simplicity, the docs and implementation could go a little bit out of sync.

@ssi02014
Copy link
Contributor Author

ssi02014 commented Jun 13, 2024

@raon0211 I've reverted the document, annotations I was working on. thank you! 🙏

Copy link
Collaborator

@raon0211 raon0211 left a comment

Choose a reason for hiding this comment

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

Thanks!

@raon0211 raon0211 merged commit 04ec2c2 into toss:main Jun 13, 2024
5 of 6 checks passed
@ssi02014 ssi02014 deleted the feat/partition branch June 13, 2024 11:30
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.

4 participants