Reputation: 2276
yesterday I was adding an enum
to our app.d.ts
file, which holds most of the global app-specific types.
The file looks roughly like this:
// app.d.ts
export module App
{
// … more definitions
enum MyEnum
{
A,
B,
C,
D
}
}
Which in itself is compiling fine. Now, if you try to use said enum in a different file like this:
// a.ts
import {App} from "./app";
let something = App.MyEnum.A;
TypeScript is complaining that it suddenly can't resolve the file app.d.ts
.
After a lot of debugging and reading through the docs, I found the following disclaimer:
Enums are real objects that exist at runtime
So when moving MyEnum
from app.d.ts
to its own MyEnum.ts
file and explicitly exporting it, it suddenly works:
// MyEnum.ts
export enum MyEnum
{
A,
B,
C,
D
}
// a.ts
import {MyEnum} from "./MyEnum";
let something = MyEnum.A;
So now my real question is: Is this due to the fact that TypeScript doesn't inline enum values into constants (unless you're using const enum
) but keeps them around as real objects, and since the .d.ts
files are basically thrown away when compiling TypeScript into JavaScript, it fails with this very weird and sort of unrelated error message?
Upvotes: 14
Views: 5982
Reputation: 1442
As I just had the very same error and your question here helped me fix it right away (thanks!), I assume that the answer to your question would be: Yes, exporting/importing enum
s from .d.ts
files does not work and causes this somewhat misleading error.
What works though, is putting a .ts
file with the same name in the same place, so that in your case for example app.d.ts
and app.ts
co-exist in the same folder. This lets you import the enum
without having to change the import. Import still works with import { MyEnum } from "./app";
in your case.
Upvotes: 14