Skip to content

Use --preserveConstEnums or switch to non-const enums #5070

@DanielRosenwasser

Description

@DanielRosenwasser

The TypeScript team largely regards const enums as a design mistake because it causes so many downstream issues with libraries.

One issue I've personally run into with Fluid though relates to the fact that they don't work with the single-file at a time compilation model that lots of tools use including

  • TypeScript's transpileModule (which is surfaced by ts-loader and awesome-typescript-loader)
  • Babel
  • esbuild
  • swc

As an example, if you try to write a line of code like

import { IMergeTreeOp, MergeTreeDeltaType } from "@fluidframework/mergetree";

function mergeymerge(op: IMergeTreeOp) {
  if (op.type === MergeTreeDeltaType.INSERT) {
    // ...
  }
}

The code will fail to run correctly under several of these modes, and you will get an error under --isolatedModules.

One way to alleviate issues with const enums is to at least compile the TypeScript code with --preserveConstEnums, so that these exports exist at runtime or bundle-time. These can increase your bundle size if you're not using TypeScript, but it will make running the code more accessible.

Metadata

Metadata

Assignees

Labels

area: buildBuild related issuesbugSomething isn't working

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions