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 typing performance regression #2166

Merged
merged 1 commit into from Apr 8, 2024
Merged

Conversation

felixmeziere
Copy link
Contributor

@felixmeziere felixmeziere commented Apr 6, 2024

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.

Thanks for the time and effort to find a fix for issue #2138 @felixmeziere!

I run the test locally and no errors were found.
@adrai when you have the change could you please approve workflow run so we can double check on GitHub actions?.

Reviewing the code I found a possible improvement on FilterKeysByContext.
@felixmeziere Could you please try if the change works also in your project?

Comment on lines 119 to 123
type FilterKeysByContext<Keys, Context extends any> = Context extends string
? Keys extends `${infer Prefix}${_ContextSeparator}${Context}${infer Suffix}`
? `${Prefix}${Suffix}`
: never
: Keys;
Copy link
Member

Choose a reason for hiding this comment

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

FilterKeysByContext is only used by ParseKeys which already check that Context value is a string before so it can be changed to something like this:

type FilterKeysByContext<Keys, Context extends string> = 
   Keys extends `${infer Prefix}${_ContextSeparator}${Context}${infer Suffix}`
    ? `${Prefix}${Suffix}`
    : never;

With this change we can also remove extends any which doesn't add any type specifity

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@marcalexiei thanks, actually the most important part is the const ... so I improved the fix further so that the code is more readable (amended the commit sorry).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I applied code formatting which changed other places in the file, not sure if it's legit or not, let me know :)

Copy link
Member

Choose a reason for hiding this comment

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

actually the most important part is the const ... so I improved the fix further so that the code is more readable

👌


I applied code formatting which changed other places in the file, not sure if it's legit or not, let me know :)

Strange, code base already follows formatting rules.
Actually these changes do not follow prettier styles so related PR check is failing.
Try running:

npm run format:fix

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done thanks, for some reason my IDE wouldn't pick up the proper prettier config...

@coveralls
Copy link

coveralls commented Apr 6, 2024

Coverage Status

coverage: 96.197%. remained the same
when pulling 9487c00 on GreenGoTech:master
into 0949a2b on i18next:master.

@felixmeziere felixmeziere force-pushed the master branch 3 times, most recently from 555c9b7 to c708c3a Compare April 8, 2024 06:47
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.

Everything checks out. Thank you again!

@felixmeziere felixmeziere changed the title [Maybe?] Fix typing performance regression Fix typing performance regression Apr 8, 2024
@felixmeziere felixmeziere marked this pull request as ready for review April 8, 2024 12:38
@adrai adrai merged commit 3735a89 into i18next:master Apr 8, 2024
9 checks passed
@adrai
Copy link
Member

adrai commented Apr 8, 2024

Thank you... it's included in v23.11.0

@@ -128,12 +128,13 @@ export type ParseKeys<
KPrefix = undefined,
Keys extends $Dictionary = KeysByTOptions<TOpt>,
ActualNS extends Namespace = NsByTOptions<Ns, TOpt>,
const Context extends TOpt['context'] = TOpt['context'],
Copy link

@dargmuesli dargmuesli Apr 8, 2024

Choose a reason for hiding this comment

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

The added const here throws an error TS1277: 'const' modifier can only appear on a type parameter of a function, method or class in a project of mine. I suspect this shouldn't be the case?

Copy link

@Shin-Ogata Shin-Ogata Apr 9, 2024

Choose a reason for hiding this comment

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

I'm also encountering into the same problem.
I'm using typescript v5.4.4.

Copy link
Member

Choose a reason for hiding this comment

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

This issue will be tracked via #2168

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

6 participants