Skip to content

chore(i18n): improve type safety #7781

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

Merged
merged 3 commits into from
May 28, 2025
Merged

Conversation

avivkeller
Copy link
Member

Description

This PR updates our types to better reflect i18n. For example, t("...") will now only accept valid i18n keys.

Validation

Related Issues

Check List

  • I have read the Contributing Guidelines and made commit messages that follow the guideline.
  • I have run pnpm format to ensure the code follows the style guide.
  • I have run pnpm test to check if all tests are passing.
  • I have run pnpm build to check if the website builds without errors.
  • I've covered new added functionality with unit tests if necessary.

@Copilot Copilot AI review requested due to automatic review settings May 26, 2025 21:58
@avivkeller avivkeller requested a review from a team as a code owner May 26, 2025 21:58
Copy link

vercel bot commented May 26, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
nodejs-org ✅ Ready (Inspect) Visit Preview May 27, 2025 8:08pm

@avivkeller
Copy link
Member Author

image

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR enhances type safety for internationalization by introducing and applying a new IntlMessageKeys type across the codebase, ensuring that translation functions only accept valid keys.

  • Updated JSDoc in the i18n package to return a strongly typed Locale
  • Added and imported IntlMessageKeys, then asserted various label and info strings as valid keys
  • Introduced the IntlMessageKeys generic type and integrated it into types, hooks, components, and tests

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
packages/i18n/lib/index.mjs Updated JSDoc @returns to use Locale from types
apps/site/util/downloadUtils/index.tsx Cast various label fields to IntlMessageKeys for safety
apps/site/types/navigation.ts Imported IntlMessageKeys for FooterConfig.text
apps/site/types/i18n.ts Defined the IntlMessageKeys generic based on Locale
apps/site/types/blog.ts Applied IntlMessageKeys to BlogCategory
apps/site/tests/e2e/general-behavior.spec.ts Refined verifyTranslation signature to accept Locale type
apps/site/hooks/react-generic/useSiteNavigation.ts Imported IntlMessageKeys for navigation hook
apps/site/components/Downloads/Release/ReleaseCodeBox.tsx Cast info key to IntlMessageKeys and imported the type
Comments suppressed due to low confidence (2)

apps/site/components/Downloads/Release/ReleaseCodeBox.tsx:18

  • The import path includes a '.js' extension, but the type is defined in '#site/types/i18n.ts'. Update the import to '#site/types/i18n' to ensure correct module resolution.
import type { IntlMessageKeys } from '#site/types/i18n.js';

apps/site/hooks/react-generic/useSiteNavigation.ts:8

  • [nitpick] The IntlMessageKeys import is not used in this file. Remove the unused import to keep dependencies clean and avoid lint warnings.
IntlMessageKeys,

@codecov-commenter
Copy link

codecov-commenter commented May 26, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 75.33%. Comparing base (39e3ae1) to head (27f2631).
Report is 5 commits behind head on main.

✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7781      +/-   ##
==========================================
+ Coverage   75.31%   75.33%   +0.01%     
==========================================
  Files          96       96              
  Lines        7856     7862       +6     
  Branches      192      192              
==========================================
+ Hits         5917     5923       +6     
  Misses       1938     1938              
  Partials        1        1              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Signed-off-by: Aviv Keller <me@aviv.sh>
@AugustinMauroy
Copy link
Member

aviv could you apply thing such as proposed here it's look little be simpler
https://next-intl.dev/docs/workflows/typescript

@avivkeller
Copy link
Member Author

aviv could you apply thing such as proposed here it's look little be simpler

https://next-intl.dev/docs/workflows/typescript

That's what I did, see the global.d.ts file

@AugustinMauroy
Copy link
Member

That's what I did, see the global.d.ts file

But I didn't get why you need a as IntlMessageKeys

@avivkeller
Copy link
Member Author

avivkeller commented May 27, 2025

We cast in the download utilities because they are from JSON, so the JSON strings must be casted to our type.

Copy link
Contributor

github-actions bot commented May 28, 2025

Lighthouse Results

URL Performance Accessibility Best Practices SEO Report
/en 🟢 99 🟢 100 🟢 100 🟢 91 🔗
/en/about 🟢 100 🟢 100 🟢 100 🟠 82 🔗
/en/about/previous-releases 🟢 100 🟢 100 🟢 100 🟠 83 🔗
/en/download 🟢 97 🟢 100 🟢 100 🟢 91 🔗
/en/blog 🟢 100 🟢 100 🟢 96 🟢 92 🔗

@avivkeller avivkeller added this pull request to the merge queue May 28, 2025
Merged via the queue into main with commit 878bfaf May 28, 2025
19 checks passed
@avivkeller avivkeller deleted the chore/i18n/improve-type-safety branch May 28, 2025 12:17
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