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(types): typescript joinArrays support #2112

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sreucherand
Copy link

This fixes typing errors when using joinArrays on array types.

I'm not completely confident on the approach but I did my best to define a clear test suite that would demonstrate the issues I encountered. I'd be glad to discuss this through.

Here is a minimum reproducible example of the initial problem: https://codesandbox.io/p/sandbox/18n-basic-example-2-forked-7lgxrf?file=/src/App.tsx:10,11

image

Checklist

  • only relevant code is changed (make a diff before you submit the PR)
  • run tests npm run test
  • tests are included
  • commit message and code follows the Developer's Certification of Origin

@adrai
Copy link
Member

adrai commented Jan 9, 2024

@marcalexiei can you verify?

@coveralls
Copy link

Coverage Status

coverage: 91.718%. remained the same
when pulling 821d264 on sreucherand:feat-arrayaccess-joinarrays-support
into f78fa2b on i18next:master.

@marcalexiei
Copy link
Member

marcalexiei commented Jan 9, 2024 via email

Copy link
Member

@marcalexiei marcalexiei left a comment

Choose a reason for hiding this comment

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

TypeOptions#joinArrays

joinArrays should be extracted by TOptions and not set in TypeOptions.
TypeOptions contains the global configuration properties and I do not consider joinArrays one of them since it affects single t function invocation.

The only usage is inside TReturnOptionalObjects which already has a TOpt parameter from where you can extract the joinArrays type.

All changes related to this are marked with (review main comment) comment .
I hope I got all of them 😅

Porting changes to typescript v4 file

Your changes are applied just to t.d.ts.
Could you please port your code to t.v4.d.ts?

Comment on lines +49 to +51
it('should work with `count`', () => {
expectTypeOf(t('key', { count: 1 })).toEqualTypeOf<'item' | 'items'>();
});
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this test should go here. key values are just strings:

// ...
key_one: 'item';
key_other: 'items';
// ...

This scenario should be tested in misc or custom-types workspaces, if it is not already.

Comment on lines +104 to +108

/**
* A string to separate each pair of adjacent elements of the array
*/
joinArrays: false;
Copy link
Member

Choose a reason for hiding this comment

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

This should be removed (review main comment)

@@ -11,6 +17,7 @@ import type {
/* eslint @typescript-eslint/ban-types: ['error', { types: { "{}": false } }] */

// Type Options
type _JoinArrays = TypeOptions['joinArrays'];
Copy link
Member

Choose a reason for hiding this comment

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

This should be removed (review main comment)


type TReturnOptionalNull = _ReturnNull extends true ? null : never;
type TReturnOptionalObjects<TOpt extends TOptions> = _ReturnObjects extends true
type TReturnOptionalObjects<TOpt extends TOptions> = _JoinArrays extends string
Copy link
Member

Choose a reason for hiding this comment

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

This should be change to rely on TOpt type parameter (review main comment)

Suggested change
type TReturnOptionalObjects<TOpt extends TOptions> = _JoinArrays extends string
type TReturnOptionalObjects<TOpt extends TOptions> = TOpt['joinArrays'] extends string

@adrai
Copy link
Member

adrai commented Feb 17, 2024

@sreucherand any update on this?

@sreucherand
Copy link
Author

Hi @adrai , I'll get back on this asap, sorry about the delay.

Copy link

stale bot commented Mar 17, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Mar 17, 2024
@adrai
Copy link
Member

adrai commented May 2, 2024

@sreucherand any update on this?

@stale stale bot removed the stale label May 2, 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.

None yet

4 participants