Skip to content

fix(typing): n() & d() output depending "part" option #2193

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 2 commits into from
May 27, 2025

Conversation

pinguet62
Copy link
Contributor

@pinguet62 pinguet62 commented May 26, 2025

fix #2192

Hello 🖖

I don't know how to fix 2 failings existing tests

  • expectTypeOf( d<Date, string>(..., {..., part: true }) ).toEqualTypeOf<Intl.DateTimeFormatPart[]>()
  • expectTypeOf( n<string>(..., {..., part: true }) ).toEqualTypeOf<Intl.NumberFormatPart[]>()

Probably, declare the 2nd generic type as string is in conflict with { part: true } type inference.
The correct usage should be type inference, without generic declaration.
So in my opinion, like these tests was added in PR introducing regression, they are not correct, and can be removed.
Please could you confirm?

Summary by CodeRabbit

Summary by CodeRabbit

  • Refactor

    • Improved type precision for date and number formatting methods, ensuring return types depend on the presence of a part option.
    • Simplified method signatures for formatting functions in type declarations.
  • Tests

    • Updated type assertions in tests to reflect refined return types and simplified generic parameters.
  • Documentation

    • Enhanced type definitions for better clarity in developer-facing type declarations.

Copy link

coderabbitai bot commented May 26, 2025

Walkthrough

The changes update type definitions for date and number formatting methods in the core and Vue integration packages. They introduce a conditional type alias to dynamically determine the return type based on the presence and value of a part property in options, and update related test assertions to reflect the more precise typing.

Changes

File(s) Change Summary
packages/vue-i18n-core/src/composer.ts, packages/vue-i18n/src/vue.d.ts Introduced IsPart conditional type; updated formatting method signatures to use conditional return types.
packages/vue-i18n-core/test/composer.test-d.ts Updated type assertions and removed redundant generics in tests to align with new conditional typing.

Assessment against linked issues

Objective Addressed Explanation
Typing for n() and d() should be dynamic: string unless { part: true } option is set (#2192)

Poem

A rabbit hops through types anew,
Where numbers, dates, and options grew.
If "part" is true, arrays abound,
If not, a string is what is found.
With clever types, the code’s refined—
A typist’s dream, well-designed!
🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fa5761c and b1fe8de.

📒 Files selected for processing (1)
  • packages/vue-i18n-core/test/composer.test-d.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/vue-i18n-core/test/composer.test-d.ts
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/vue-i18n/src/vue.d.ts (1)

607-620: Minor type parameter inconsistency.

The OptionsType generic is defined with a default value but the actual options parameter on line 618 still uses DateTimeOptions<Key | ResourceKeys> directly instead of OptionsType. Consider updating line 618 to use OptionsType for consistency:

-      options: DateTimeOptions<Key | ResourceKeys>,
+      options: OptionsType,
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b9109c0 and 90969de.

📒 Files selected for processing (3)
  • packages/vue-i18n-core/src/composer.ts (5 hunks)
  • packages/vue-i18n-core/test/composer.test-d.ts (2 hunks)
  • packages/vue-i18n/src/vue.d.ts (7 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
packages/vue-i18n/src/vue.d.ts (2)
packages/core-base/src/datetime.ts (1)
  • DateTimeOptions (80-103)
packages/core-base/src/number.ts (1)
  • NumberOptions (78-101)
packages/vue-i18n-core/src/composer.ts (3)
packages/vue-i18n-core/src/index.ts (2)
  • DateTimeOptions (7-7)
  • NumberOptions (30-30)
packages/core-base/src/datetime.ts (1)
  • DateTimeOptions (80-103)
packages/core-base/src/number.ts (1)
  • NumberOptions (78-101)
🔇 Additional comments (8)
packages/vue-i18n-core/test/composer.test-d.ts (3)

9-9: LGTM!

The addition of NumberOptions import is necessary for the updated type assertions.


349-364: Good fix for the type inference issue.

The removal of the third generic parameter aligns with the PR objectives. The type system can now correctly infer the return type based on the part option without explicit type declarations conflicting with the inference.


367-388: Test updates correctly validate the conditional return types.

The test assertions properly verify that:

  • When part: false or not specified, the methods return string
  • When part: true, the methods return the appropriate format parts array

This confirms the type inference is working as expected.

packages/vue-i18n-core/src/composer.ts (3)

245-246: Well-designed utility type for conditional return types.

The IsPart<O> type alias elegantly extracts the part property type or defaults to false. This enables the conditional return type logic needed to fix the type inference issue.


1116-1150: Excellent implementation of the conditional return type.

The changes correctly implement dynamic return types based on the part option:

  • Removed the explicit Return generic parameter that was causing conflicts
  • Uses IsPart<typeof keyOrOptions> to conditionally return either Intl.DateTimeFormatPart[] when part is truthy or string otherwise
  • Maintains backward compatibility while fixing the type inference issue

1212-1252: Consistent implementation with proper type constraints.

The number formatting interface changes mirror the datetime formatting approach:

  • Introduces OptionsType generic to capture the actual options type
  • Uses IsPart<OptionsType> for conditional return types
  • Properly constrains the generic types for type safety

This ensures consistent behavior across both formatting APIs.

packages/vue-i18n/src/vue.d.ts (2)

29-30: Consistent type alias implementation.

The IsPart type alias is correctly duplicated here for the Vue module declarations, maintaining consistency with the core composer implementation.


673-740: Correct implementation of conditional return types for $n.

The number formatting methods properly implement the conditional typing:

  • OptionsType generic is correctly used in both type parameters and method parameters
  • Return types conditionally resolve based on the part option
  • Maintains consistency with the core composer implementation

@kazupon kazupon added the Type: Bug Bug or Bug fixes label May 26, 2025
Copy link

Size Report

Bundles

File Size Gzip Brotli
core.esm-browser.prod.js 38.05 kB 11.62 kB 10.39 kB
core.global.prod.js 31.11 kB 10.78 kB 9.67 kB
core.runtime.esm-browser.prod.js 23.77 kB 7.71 kB 6.90 kB
core.runtime.global.prod.js 18.08 kB 7.04 kB 6.32 kB
message-compiler.esm-browser.prod.js 19.29 kB 5.76 kB 5.14 kB
message-compiler.global.prod.js 17.32 kB 5.55 kB 4.98 kB
petite-vue-i18n-core.esm-browser.prod.js 20.67 kB 6.96 kB 6.26 kB
petite-vue-i18n-core.global.prod.js 15.63 kB 6.06 kB 5.51 kB
petite-vue-i18n.esm-browser.prod.js 36.98 kB 11.34 kB 10.17 kB
petite-vue-i18n.global.prod.js 29.84 kB 10.19 kB 9.20 kB
petite-vue-i18n.runtime.esm-browser.prod.js 22.55 kB 7.33 kB 6.63 kB
petite-vue-i18n.runtime.global.prod.js 16.83 kB 6.40 kB 5.80 kB
vue-i18n.esm-browser.prod.js 50.68 kB 15.15 kB 13.55 kB
vue-i18n.global.prod.js 40.54 kB 13.61 kB 12.25 kB
vue-i18n.runtime.esm-browser.prod.js 36.25 kB 11.15 kB 10.02 kB
vue-i18n.runtime.global.prod.js 27.53 kB 9.85 kB 8.89 kB

Usages

Name Size Gzip Brotli
packages/size-check-core (@intlify/core) 9.22 kB 3.82 kB 3.46 kB
packages/size-check-petite-vue-i18n (petite-vue-i18n) 77.63 kB 30.24 kB 27.28 kB
packages/size-check-vue-i18n (vue-i18n) 82.85 kB 31.74 kB 28.64 kB

Copy link

pkg-pr-new bot commented May 26, 2025

Open in StackBlitz

@intlify/core

npm i https://pkg.pr.new/@intlify/core@2193

@intlify/core-base

npm i https://pkg.pr.new/@intlify/core-base@2193

@intlify/devtools-types

npm i https://pkg.pr.new/@intlify/devtools-types@2193

@intlify/message-compiler

npm i https://pkg.pr.new/@intlify/message-compiler@2193

@intlify/shared

npm i https://pkg.pr.new/@intlify/shared@2193

petite-vue-i18n

npm i https://pkg.pr.new/petite-vue-i18n@2193

vue-i18n

npm i https://pkg.pr.new/vue-i18n@2193

@intlify/vue-i18n-core

npm i https://pkg.pr.new/@intlify/vue-i18n-core@2193

commit: b1fe8de

@kazupon
Copy link
Member

kazupon commented May 26, 2025

Welcome contribution!
I'm happy to your contributrion!
But, This PR has unit test errors. Could you resolve it? 🙏

@kazupon kazupon merged commit d4c6e3a into intlify:master May 27, 2025
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Bug or Bug fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression with "Part options support for $n"
2 participants