0

I have a problem with Typescript const enums in .svelte files in my project. I have a type definition file, shared between multiple parts of my project. There are two namespaces (Events and Procedures) in here, each consists of 3-4 enums with event/procedure names, one for every part of the project.

So the structure looks like this.

namespace Shared {
  namespace Events {
    const enum Server {
       BC = "DF",
       ...
    }
    const enum Client {
       AB = "DB",
       ...
    },
    const enum SomethingElse {
       ...
    }
    ...
  },
  namespace Procedures {
    ...
  }
}

This approach works great for every part of the project -- except svelte files. So if I'm trying to use Shared.Events.Server.BC in any other environment (including .ts files of the same "part"), I get DF. If I'm trying to use it in .svelte files, after bundling, in the resulting .js file, it stays untouched: Shared.Events.Server.BC. (And since there is no Shared in the runtime, it's not working).

I can't switch to plain enums due to the technical limitations of other parts of the project.

For bundling I'm using Rollup, svelte-preprocess and @rollup/plugin-typescript with a pretty much standard configuration:

    svelte({
        dev: isDevelopment,
        extensions: [".svelte"],
        preprocess: sveltePreprocessor(),
        emitCss: true,
        css: function(css) {
            css.write(path.join(dest, 'bundle.css' ));
        },
        onwarn: (warning, handler) => {
            if (warning.code.indexOf("a11y") !== -1) return;
            handler(warning);
        }
    }),

    typescript({ typeRoots: ["./types/", "./node_modules/@types/"]  }),
    resolve(),
    commonjs({ include: "node_modules/**", extensions: [".js", ".ts"] }),
    ...

F'1
  • 324
  • 1
  • 4
  • I think this is not supported right now. `svelte-preprocess` transpiles each Svelte file inidividually without knowing the "outside world", so it cannot know that `Shared.Events.Server.BC` resolves to `"DF"`. – dummdidumm Sep 07 '20 at 06:53
  • Keep in mind that it's not recommended to mix es6 and namespaces. Check this [SO answer](https://stackoverflow.com/a/37572180/8583669). – johannchopin Sep 07 '20 at 08:03

0 Answers0