Enhance TransformContext typing with conditional types for better type safety#6017
Enhance TransformContext typing with conditional types for better type safety#6017
Conversation
|
|
✅ Deploy Preview for rsbuild ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Co-authored-by: chenjiahan <7237365+chenjiahan@users.noreply.github.com>
|
@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 <7237365+chenjiahan@users.noreply.github.com>
✅ PR completed! Fixed the Changes made in commit 8ad5d9a:
|
packages/core/src/types/plugin.ts
Outdated
| * When raw is true, this will be a Buffer instead of a string. | ||
| */ | ||
| code: string; | ||
| code: string | Buffer; |
There was a problem hiding this comment.
@copilot can we ensure that code is string when raw option is not used?
There was a problem hiding this comment.
✅ 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 <7237365+chenjiahan@users.noreply.github.com>

The
TransformContexttype has been enhanced with conditional typing to provide better type safety based on therawoption in transform descriptors.Problem:
Previously, the
codeproperty 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
rawoption: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.