awesome-typescript-loader icon indicating copy to clipboard operation
awesome-typescript-loader copied to clipboard

Cannot import an enum from a declaration file ("TypeScript declaration files should never be required")

Open karol-majewski opened this issue 7 years ago • 2 comments

Hello and thank you for your work.

Inside my project, I have a directory full of types called typings. I have created an alias (@typings) for it in both tsconfig.json and in my Webpack config. Everything was working fine until I tried to export an enum from a declaration file.

In the beginning, it didn't work at all, because the barrel file's (index.d.ts) extension was not on the list of resolved file types.

Module not found: Error: Can't resolve '@typings' in ...

I added it to the list of resolved file types:

 resolve: {
-  extensions: ['.js', '.jsx', '.ts', '.tsx', 'json'],
+  extensions: ['.js', '.jsx', '.ts', '.d.ts', '.tsx', 'json'],

Which caused the warning to pop up.

https://github.com/s-panferov/awesome-typescript-loader/blob/bec43a0b127c242be0398070ba27e511665e5106/src/index.ts#L53-L56

My question is: is this intended, or should one be allowed to import enums as values from declaration files?

Usage

Seems related: https://github.com/s-panferov/awesome-typescript-loader/issues/422

karol-majewski avatar Feb 20 '18 00:02 karol-majewski

Maybe I dont undestand something but I'm not sure what do you mean by:

until I tried to export an enum from a declaration file.

.d.ts shouldn't be added to resolve. What's the point? You should require js and .d.ts is read by a compiler. .d.ts theoretically shouldn't contain "export enum". it should be "export declare enum".

sherlock1982 avatar Mar 08 '18 17:03 sherlock1982

Probably not the same error ("TypeScript declaration files should never be required"), but may be related (?)

I'm really a noob on ts, so i guess I'm doing something wrong, but on my project when i import the enum SocketEvents on the front/src/front.ts using import {PlayerDirections, SocketEvents} from '../../app-defs'; i get a Module not found: Error: Can't resolve '../../app-defs' in 'G:\Projects\ts-phaser-bomb-game\front\src', and when i only import the PlayerDirections everything compiles fine... is that a intentional?

This comment fixed for me: https://github.com/ng-packagr/ng-packagr/issues/809#issuecomment-421831045

I was getting the same error and adding const to my enum made the difference.

Grohden avatar Mar 28 '19 02:03 Grohden