-
Notifications
You must be signed in to change notification settings - Fork 150
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
const enum
in a component always generates a runtime object
#281
Comments
I ran into this as well. I believe this is due to a bug in TypeScript, not svelte-preprocess: Further discussion in recent design meeting notes: |
@harvey-k I think you're referring to some other problem. The issues you linked to say that My issue is that const enum defined inside the component is not inlined and behaves like you explicitly set So you do something like <script>
const enum States {
loading,
error,
data,
}
let state: States = States.loading;
</script>
{#if state == States.loading}
...
{:else if state == States.error}
...
{:else}
...
{/if} and you would expect it to be inlined like (function (States) {
States[States["loading"] = 0] = "loading";
States[States["error"] = 1] = "error";
States[States["data"] = 2] = "data";
})(States || (States = {})); |
@dkzlv Sorry, I might have linked an issue only partially related to this complicated mix. Here's a standalone example: index.ts const enum State {
Loading = 1,
Error = 2,
Data = 3
};
export function load() {
let s: State = State.Loading;
switch (+s) {
case State.Loading:
console.log("loading");
break;
case State.Error:
console.log("error");
break;
case State.Data:
console.log("data");
break;
default:
console.log("unknown");
break;
}
} Using typescript 4.2.3 produces the following index.js
"use strict";
exports.__esModule = true;
exports.load = void 0;
var State;
(function (State) {
State[State["Loading"] = 1] = "Loading";
State[State["Error"] = 2] = "Error";
State[State["Data"] = 3] = "Data";
})(State || (State = {}));
;
function load() {
var s = State.Loading;
switch (+s) {
case State.Loading:
console.log("loading");
break;
case State.Error:
console.log("error");
break;
case State.Data:
console.log("data");
break;
default:
console.log("unknown");
break;
}
}
exports.load = load; Not using isolatedModules produces our hoped for output
"use strict";
exports.__esModule = true;
exports.load = void 0;
;
function load() {
var s = 1 /* Loading */;
switch (+s) {
case 1 /* Loading */:
console.log("loading");
break;
case 2 /* Error */:
console.log("error");
break;
case 3 /* Data */:
console.log("data");
break;
default:
console.log("unknown");
break;
}
}
exports.load = load; |
Unfortunately, this really won't work because the preprocessor only handles typescript inside the |
Also, it appears that
|
@harvey-k That is eye opening, thanks. @kaisermann Yeah, I see. Don't really think it would be worth the effort to start working with TS inside the template. I've only come up with two cases when it's useful (non-null assertion and const enums), and there's always a way to do mostly the same but without the problems for the compiler and library. Tried to find a way around this limitation, but no luck. Added a related feature request in the main svelte repo, that would help terser inline objects like this in the const States = {
error: 0,
loading: 1,
data: 2
} It would fix the issue for me entirely. For now it's not inlined due to the order of declarations in the compiled Svelte code, more on this here: sveltejs/svelte#6062. For now I think it's reasonable to close the issue as it cannot be dealt with in any way. |
Describe the bug
If you use a
const enum
in the<script>
tag it will still generate a runtime object.To Reproduce
terser
rollup plugin (to have readable output). Also, even though the default state ofpreserveConstEnums
isfalse
, you can set it just to be sure.App.svelte
contents with this:npm build
or just look into IDE tab with the pre-built version of the component. It will look like this:Expected behavior
Should be built into something like this:
Additional context
Copied from here.
@dummdidumm suggested that the reason is the
isolatedModules
setting. I tried to set it intofalse
, no luck. Also it seems to me that if you do not export theconst enum
isolatedModules
doesn't seem to affect it at all (based mostly on this article).I tried to compile the following TS file with and without the
isolatedModules
setting:The result seems to be the same (and correct):
The text was updated successfully, but these errors were encountered: