You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TypeScript's (legacy) prefix style type assertions (<Type>val instead of (val as Type)) cannot always be safely removed in "strip-only" mode when they are at the start of the body of an arrow function.
TypeScript's (legacy) prefix style type assertions (val instead of (val as Type)) cannot always be safely removed in "strip-only" mode when they are at the start of the body of an arrow function.
Hi @kdy1, thanks for taking a look. In my testing it looks like "strip-types" does remove it.
(() => <any>{}) becomes (() => {}). The first returns {} at runtime, the second returns undefined.
For the case where the arrow function body only contains the type-assertion and object literal I think there is a safe transform to: (() => 0||{}), which keeps the original JS in the same position with the same semantics. However there are niche inputs which I think don't have safe position-preserving transforms such as: (() => <T>{p:null}.p??g), though I admit the chances of someone writing that is low.
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.
Describe the bug
TypeScript's (legacy) prefix style type assertions (
<Type>val
instead of(val as Type)
) cannot always be safely removed in"strip-only"
mode when they are at the start of the body of an arrow function.Input code
Config
Playground link (or link to the minimal reproduction)
no playground support for wasm-typescript yet: swc-project/swc-playground#42
SWC Info output
No response
Expected behavior
Either error or (if possible) produce JS with matching semantics as the input i.e. the equivalent of:
() => ({})
;Actual behavior
Resulting code is:
(() => {})
which has different semantics to the input.Version
@swc/wasm-typescript@1.7.0
Additional context
I think it's not possible to solve this in all cases. So it might be best to error for arrow functions that start with
<Type>{
.There are some cases that can work with
"strip-only"
:can strip and keep the JS in place with:
But that approach does not work for:
which could not be changed to:
As it is invalid to have
||
and??
at the same level.I would be very interesting if there was a way to solve this, otherwise I think the solution is to error.
The text was updated successfully, but these errors were encountered: