Warnings when using an exported interface in a class
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.
@th0r why don't you try it out? I think you'll find that the result is empty, despite your expectations. Keep in mind you are using the class here as a type annotation only, and more importantly you are not explicitly re-exporting it. From the outside all that is exported is the interface which consists of type annotations and nothing more.
@mischkl I'm sorry! You're right - just tried to extract them into separate files again and warnings have disappeared. Seems like I did something wrong first time 🤔
@filipesilva is there a typescript issue for this that can be referenced by this issue for future tracking if it gets fixed?
@zakhenry not that I know of, no.
~~Is there a reason why these warnings fail execution in the browser when its just warnings and not errors? As mentioned by someone in [email protected] it seemed no showstopper for development but as of [email protected] it halts development completely when it happens. My team is in the progress of migrating an app with 150+ components and suddenly this issue appeared for us. I'm still goofing around trying to find workarounds. It seems to have appeared completely random after having migrated roughly 30 components..~~ EDIT: Upgrading first helps.. had a specific version entered in package.json facepalm
So far I've found that any kind of union type on affected decorated properties will remove the warning, e.g.
@Input() someProp:MyMagicalInterface
will result in a warning while
@Input() someProp:MyMagicalInterface | undefined
will work. And to get completely funky
@Input() someProp:MyMagicalInterface | MyMagicalInterface
does work also. ¯_(ツ)_/¯
for whatever it's worth ... when you change the module in the tsconfig to "commonjs" the error goes away. This may not work for most situations, in mine it does because I am using a core module that is compiled outside of the cli project (with ngc) and later linked into it.
// ./src/app/wx.d.ts
export declare function config(): void;
// ./src/app/app.component.ts
import * as wx from './wx.d';
// using
wx.config();
ng serve throw Module build failed: Error: Debug Failure. False expression: Output generation failed.
Changing tsconfig module to "commonjs" fixed the warnings for me. Previously it was set to "es2015"
is there a way to just silence that warning?
@remmeier see the comment a few above - you can suppress the warning simply by making the type a union of itself. The same type safety is achieved, and the warning goes away. It is by no means a fix, but at least you can continue developing
The issue still exists, why is this closed?
@RenaldasK there is long explanation (shortly - it is the limitation which currently cannot be solved) https://github.com/angular/angular-cli/issues/2034#issuecomment-302666897
Try to rewrite interfaces to classes if possible (this works for me). Or one of the proposed solutions above (i did not try any of them).
Aside from the console warnings, is there any actual reason to avoid this issue?
All of the solutions above feel like a hacks, and putting a single line interface in its own file seems like a waste of time...
Having the same issue with a scenario like this:
export class Something { ... }
export type SomethingKey = Pick<Something , 'keyPropertyName'>;
in one file. However when I add another interface, class or type to that file, the interface is resolved perfectly fine. This seem to be only the issue with the Pick<..> type. It works, compiles, transpiles, it only gives a warning in the CLI...
@filipesilva Thanks for looking into the problem in detail. Easiest workaround I have found is to declare a local alias type in the component file for the imported type when needed in an @Input(). So for your example, rather than:
import {TestInterface} from ...
...
@Input() test: TestInterface
do a
import {TestInterface} from ...
type TestInterfaceAliasHack = TestInterface;
...
@Input() test: TestInterfaceAliasHack
This is really annoying. Is there a real fix and not simply temporary workarounds?
This issue's very Annoying !
For the time being just a flag to silence the warnings would be great as everything works perfectly fine regardless of the warnings.
They said working on it, i have the same issue just have to wait or work with a work-around if possible.