Sphere
Log in Join
☆ 2

tsc passes but Vite build fails on a const enum

TypeScript 5.5, Vite 5.3, Vue 3. tsc --noEmit is clean. npm run build dies with: error during build: RollupError: 'Color' is not exported by src/types.ts, imported by src/components/Picker.vue. Color is a const enum in types.ts. Dev server works because esbuild transpiles per-file. Why does the production build disagree with tsc? I have seen this on two projects now and it always surprises someone.

⚑ Report

4 replies

☆ 0
P paper ·2026-10-19#1

const enums are erased at compile time by tsc, but esbuild (which Vite uses) does not inline const enums across modules the same way, it treats them as regular enums or drops them. With isolatedModules, which Vite enforces, const enums are actually not supported. Use a regular enum or a plain object/union type.

⚑ Report
☆ 0
R rust60 ·2026-10-19#2

Switched to a regular string enum and the build passed. tsc still inlines const enums but apparently esbuild per-file transpile cannot. Good to know there is a real incompatibility, not just a misconfig. isolatedModules is the key detail most people miss here.

⚑ Report
☆ 0
Y yaml91 ·2026-10-27#3

The recommended pattern with Vite is a union type plus a const object: export const Color = { Red: 'red', Blue: 'blue' } as const; export type Color = typeof Color[keyof typeof Color]. No runtime enum object, fully tree-shakeable, works with isolatedModules. We moved off enums entirely.

⚑ Report
☆ 0
T tiny22 ·2026-10-30#4

If you are in a monorepo you might also hit this with ambient const enums in .d.ts files, same root cause. We wrote a codemod to convert them to plain objects because new devs kept reintroducing const enums. It reads a bit odd at first but it is the cleanest option.

⚑ Report
Log in to reply.