yesterday I was updating an enum
in our app.d.ts
file, which contains most of the app-specific types.
The contents of the file are as follows:
// app.d.ts
export module App
{
// … more definitions
enum MyEnum
{
A,
B,
C,
D
}
}
Everything was compiling correctly. However, when attempting to use the enum in another file like this:
// a.ts
import {App} from "./app";
let something = App.MyEnum.A;
TypeScript started giving errors about not being able to resolve the file app.d.ts
.
https://i.sstatic.net/RqXVo.png
After much investigation and consulting the documentation, I came across this interesting disclaimer:
Enums are real objects that exist at runtime
This led me to move MyEnum
from app.d.ts
to its own file MyEnum.ts
, where it needed to be explicitly exported for things to work smoothly:
// MyEnum.ts
export enum MyEnum
{
A,
B,
C,
D
}
// a.ts
import {MyEnum} from "./MyEnum";
let something = MyEnum.A;
So my question now is: Could this issue be related to TypeScript not directly substituting enum values with constants (unless using const enum
), but rather treating them as persistent objects? And since .d.ts
files get discarded during compilation into JavaScript, could this be causing the unexpected error message?