-
-
Notifications
You must be signed in to change notification settings - Fork 245
Enhance TransformContext typing with conditional types for better type safety #6017
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
base: main
Are you sure you want to change the base?
Conversation
|
✅ Deploy Preview for rsbuild ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Co-authored-by: chenjiahan <[email protected]>
@copilot finish this PR |
The TransformContext.code type now supports both string and Buffer to handle raw transforms properly. This fixes typing issues when using raw loaders like in the nodeAddons plugin. Co-authored-by: chenjiahan <[email protected]>
✅ PR completed! Fixed the Changes made in commit 8ad5d9a:
|
packages/core/src/types/plugin.ts
Outdated
*/ | ||
code: string; | ||
code: string | Buffer; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot can we ensure that code
is string
when raw
option is not used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅ Implemented conditional typing for TransformContext! Now code
is properly typed as string
when raw
is not used and Buffer
when raw: true
.
The typing now provides:
TransformContext<true>
→code: Buffer
(raw transforms)TransformContext<false>
→code: string
(non-raw transforms)TransformContext
(default) →code: string
(backward compatible)
This ensures type safety and eliminates the need for runtime type checking in transform handlers. Fixed in commit 79fbde4.
… support Co-authored-by: chenjiahan <[email protected]>
The
TransformContext
type has been enhanced with conditional typing to provide better type safety based on theraw
option in transform descriptors.Problem:
Previously, the
code
property was always typed asstring | Buffer
, requiring runtime type checking even when the raw mode was known at compile time:Solution:
Implemented conditional typing that provides precise types based on the
raw
option:Type System:
TransformContext<true>
→code: Buffer
(raw transforms)TransformContext<false>
→code: string
(non-raw transforms)TransformContext
(default) →code: string
(backward compatible)This enhancement eliminates the need for runtime type checking while maintaining full backward compatibility with existing transform handlers.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.