swc-project/swc · error
flow type cast
Error message
flow type cast
What it means
The estree TypeCastExpression is Flow's `(value as T)`-style cast (Babel Flow output). SWC's AST has no Flow cast expression (only TsAsExpr for TS), so Swcify for TypeCastExpression is declared with Output = Never and immediately panics with `unimplemented!("flow type cast")`.
Solutions
- Run Flow type stripping (@babel/plugin-transform-flow-strip-types) on the AST/source before swcify
- Parse the code as TypeScript when the casts are actually TS `as` casts
- Drop or replace TypeCastExpression nodes with their inner expression in a pre-conversion pass
Example fix
// before const n = (x: number); // Flow cast -> TypeCastExpression -> panic // after const n = x; // after flow-strip-types, or parse as TS: x as number
Defensive patterns
Strategy: validation
Validate before calling
// Node: strip Flow types before swcify
const stripped = require('@babel/core').transformSync(code, {
plugins: ['@babel/plugin-transform-flow-strip-types'],
code: true,
}).code; Type guard
function isFlowTypeCast(node) {
return node.type === 'TypeCastExpression';
} Prevention
- Treat TypeCastExpression nodes as poison for swcify — strip or unwrap them first
- Parse new sources with the TypeScript syntax when possible
- Keep a Flow-fixture test in pipelines that accept community-supplied code
When it happens
Trigger: Running swcify on a Babel AST produced by a Flow-mode parse that contains Flow type cast expressions, e.g. `(x: MyType)` or `(x >>> null)`-style Flow annotations.
Common situations: Converting Flow codebases (legacy React Native, old Facebook packages) through Babel into SWC-based tooling without first stripping Flow types.
Related errors
AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17).
Data as JSON: /api/errors/58e8fdce493d6534.
Report an issue: GitHub.
Appendix: source
Thrown at crates/swc_estree_compat/src/swcify/expr.rs:842
OptChainExpr {
span: ctx.span(&self.base),
optional: self.optional,
base: Box::new(OptChainBase::Call(OptCall {
span: ctx.span(&self.base),
callee: self.callee.swcify(ctx),
args: self.arguments.swcify(ctx).into_iter().flatten().collect(),
type_args: self.type_parameters.swcify(ctx).map(From::from),
..Default::default()
})),
}
}
}
impl Swcify for TypeCastExpression {
type Output = Never;
fn swcify(self, _: &Context) -> Self::Output {
unimplemented!("flow type cast")
}
}
impl Swcify for swc_estree_ast::JSXElement {
type Output = swc_ecma_ast::JSXElement;
fn swcify(self, ctx: &Context) -> Self::Output {
swc_ecma_ast::JSXElement {
span: ctx.span(&self.base),
opening: self.opening_element.swcify(ctx),
children: self.children.swcify(ctx),
closing: self.closing_element.swcify(ctx),
}
}
}
impl Swcify for swc_estree_ast::JSXOpeningElement {
type Output = swc_ecma_ast::JSXOpeningElement;View on GitHub (pinned to 5176682b65)