angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Warnings when using an exported interface in a class

Open Maistho opened this issue 10 years ago • 140 comments

1. OS

Arch Linux

Linux maistho-laptop 4.7.2-1-ARCH #1 SMP PREEMPT Sat Aug 20 23:02:56 CEST 2016 x86_64 GNU/Linux

2. Versions

angular-cli: 1.0.0-beta.11-webpack.8
node: 6.5.0
os: linux x64

3. Repro steps

I started a new repository, and added a new empty service, with an exported interface.

Complete changes, and a repo to pull and see the issue, can be found here:

https://github.com/Maistho/angular-cli-input-issue/commit/e5c483081bfd7af3dafd0db074179b3090668e27

After running ng build, I get two warnings in the Terminal

4. The log given by the failure

Hash: 354e709ba2cf588db59a                                                                                                                                                                                          
Version: webpack 2.1.0-beta.21
Time: 11161ms
            Asset       Size  Chunks             Chunk Names
   main.bundle.js    2.51 MB    0, 2  [emitted]  main
 styles.bundle.js    10.2 kB    1, 2  [emitted]  styles
        inline.js    5.53 kB       2  [emitted]  inline
         main.map     3.1 MB    0, 2  [emitted]  main
       styles.map    14.1 kB    1, 2  [emitted]  styles
       inline.map    5.59 kB       2  [emitted]  inline
       index.html  489 bytes          [emitted]  
assets/.npmignore    0 bytes          [emitted]  
chunk    {0} main.bundle.js, main.map (main) 2.46 MB {1} [initial] [rendered]
chunk    {1} styles.bundle.js, styles.map (styles) 9.96 kB {2} [initial] [rendered]
chunk    {2} inline.js, inline.map (inline) 0 bytes [entry] [rendered]

WARNING in ./src/app/app.component.ts
18:55 export 'TestInterface' was not found in './test.service'

WARNING in ./src/app/app.component.ts
18:88 export 'TestInterface' was not found in './test.service'
Child html-webpack-plugin for "index.html":
         Asset     Size  Chunks       Chunk Names
    index.html  2.82 kB       0       
    chunk    {0} index.html 357 bytes [entry] [rendered]

5. Mention any other details that might be useful.

I could not reproduce the issue without adding the @Input() to the class member.

Compiling with tsc does not produce any warnings.

Maistho avatar Sep 09 '16 17:09 Maistho

This issue is very annoying. Could you please fix it?

scheysni avatar Feb 28 '17 16:02 scheysni

Also waiting this issue to be fixed since a long time..

broij avatar Feb 28 '17 16:02 broij

If you extend an interface the warning remains when you split the interfaces in files image

stefanaerts avatar Mar 08 '17 12:03 stefanaerts

This is getting more attention from users than it should, and less attention from maintainers as I see the dates. Anyway, I come here not to complain (since I know the problem is hard), but to provide a valid workaround.

Our team uses a lot of interfaces. But also a lot of barrels (those index files where you simply re-export public stuff). A workaround that is working perfectly for us is using explicit re-exports, i.e:

Instead of using export * from './file-with-interfaces'; in that index.ts, we do export { SomeClass, SomeType, AnInterface1, AnInterface2 } from './file-with-interfaces';

Later using import { SomeClass, AnInterface1, OtherClass } from './the-folder-with-barrel'; works in the app and shows no warnings. I expect this may help some sorrowful souls ;).

PS: we have 62 interfaces and 0 warnings with this method ;)

ShadowManu avatar Mar 08 '17 14:03 ShadowManu

I made the same suggestion as @ShadowManu just above and got 3 negative votes - it 'd be nice if you explained your reasons :+1:

anodynos avatar Mar 08 '17 22:03 anodynos

@anodynos Fragmenting your code into double the number of files to use an Angular-ideomatic OpaqueToken is sub-optimal. As I mentioned, I come from the Java world and am fairly used to the "one file, one interface" style, but in this case it really feels like the wrong thing to do. (And from a language perspective, it should just work).

wulfsberg avatar Mar 08 '17 22:03 wulfsberg

Vanilla ES6/ES7 rules!!!

** ducks **

tcoz avatar Mar 09 '17 00:03 tcoz

@anodynos there's a couple of differences though. 1) you're not forced to put interfaces aside. Just put them where you deem necessary. 2) The barrel with explicit re-exports is somehow (yeah, I don't get the particular reason) the one making the warnings go away.

ShadowManu avatar Mar 09 '17 00:03 ShadowManu

@anodynos @ShadowManu The solutions you suggest don't solve the problem, they are just workarounds:

  • @anodynos I do want to keep the OpaqueToken and interface in the same file;
  • @ShadowManu I do not want to be forced to use barrels. As it was stated by @wulfsberg , it should just work from a language perspective and I think this is the main reason why you are getting thumbs down. Personally, I prefer getting the warning messages than adopting any of those two workarounds.

broij avatar Mar 09 '17 09:03 broij

Thanks @JulienBroi - some some comments:

  • You dont need the OpaqueToken & IOpaqueToken or whatever in the same file - you can have an opaque-token-interfaces.ts or opaque-token-types.ts and stick all the interface & type related stuff in there.

  • What wrong with barrels?

anodynos avatar Mar 09 '17 11:03 anodynos

I don't need it, but I want it, the language allows it, and Angular recommends it. Insisting that I have to do it in another way because of a bug is not a good solution.

wulfsberg avatar Mar 09 '17 11:03 wulfsberg

It is a workaround and shouldn't be needed. But for those desperate, there's something that can be done in the meantime. Reverting is later as simple as deleting the files and following the broken imports.

ShadowManu avatar Mar 09 '17 15:03 ShadowManu

Could a maintainer lock this issue? I'm getting my inbox spammed and none of these comments are helpful.

MazeChaZer avatar Mar 09 '17 20:03 MazeChaZer

@MazeChaZer All you need to do is to click on the "Unsubscribe" button above on the side.

catull avatar Mar 11 '17 17:03 catull

@catull I would like to get notified when this issue is resolved

MazeChaZer avatar Mar 12 '17 17:03 MazeChaZer

For the record, the corresponding issue in webpack is webpack/webpack#2977.

As @hccampos explains there, it touches classes with decorators (annotations):

classes that contain decorators and are using an interface imported from another file trigger the warning. While everything works normally, it would be nice to get rid of the warnings.

Personally I can say it is actually only for the class of a class member annotated with @Input that I see the problem, it is not because of the class itself:

@Input() component: IComponentRow;

will trigger:

WARNING in ./src/app/features/cockpit/workspaces/petals-content/petals-component-view/petals-component-overview/petals-component-overview.component.ts
46:50-63 "export 'IComponentRow' was not found in '../../../state/components/component.interface'

The problem is not because of webpack but most certainly because of something typescript/decorators related (as explained by @sokra: https://github.com/webpack/webpack/issues/2977#issuecomment-245887841). The interface is referenced in the javascript code generated by typescript and thus webpack can't find it (since they have no real existence in the javascript realm).

@filipesilva so I think the problem is coming from typescript itself.

victornoel avatar Mar 15 '17 13:03 victornoel

I have a similar situation. I have multiple interfaces in the same file, all exported. From other files I import those and get a warning that it cannot find them. If I split each interface to its own file, the warning goes away.

Sometimes I have interfaces and classes in the same file, same result when importing the interfaces.

368:57-64 "export 'IConfig' was not found in '../models/app-insights'
export interface IAppInsights {
 // ...
}
import { IConfig } from '../models/app-insights';
// ...

Things that worked:

  1. split each interface to its own file
  2. make them abstract classes

VS Code is happy with this. VS Code can find the import, can navigate to the definition. So it seems to be OK using TypeScript ... but the compiler is not.

cc @alexeagle as an FYI

Ths appears to be a TypeScript thing. ?

johnpapa avatar Apr 13 '17 11:04 johnpapa

we changed our tsconfig.js module from es6 to commonjs and warnings were gone, based on generated code I'm not sure whats different.

Input decorator code with es6: __decorate([ webpack_require.i(WEBPACK_IMPORTED_MODULE_0__angular_core["Input"])(), __metadata('design:type', (typeof (_a = typeof WEBPACK_IMPORTED_MODULE_1__models_user_info["UserInfo"] !== 'undefined' && WEBPACK_IMPORTED_MODULE_1__models_user_info["UserInfo"]) === 'function' && _a) || Object) ], HeaderComponent.prototype, "user", void 0);

Input decorator code with commonjs: __decorate([ core_1.Input(), __metadata('design:type', (typeof (_a = typeof user_info_1.UserInfo !== 'undefined' && user_info_1.UserInfo) === 'function' && _a) || Object) ], HeaderComponent.prototype, "user", void 0);

pajics avatar Apr 13 '17 13:04 pajics

We need a better way to isolate which layer is causing the problem - it seems like angular CLI magic is making this difficult. Perhaps a minimal repro without the CLI would help?

alexeagle avatar Apr 18 '17 15:04 alexeagle

@alexeagle I dont have a minimal repro, though I will try to find a better one.

In the meantime, if you clone this repo and branch, run npm i, then run ng b you will see the issues I mentioned.

https://github.com/projectkudu/AzureFunctionsPortal/tree/upgrade-angular-v4

johnpapa avatar Apr 18 '17 15:04 johnpapa