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.
Sorry, I won't have time to dig in today. The warning does come from webpack, right? Could it be that TypeScript has erased the interface from the output .js file, but some other code still references it? (try pasting the .ts content in the TypeScript playground to see what it emits)
No worries, no rush. It's a warning, not an error.
The typescript compiler outputs an empty file. But there are other interfaces in that file which are fine.
Another interesting observation is that if I move each interface to its own file, the warning goes away
I am also experiencing the same thing, but it happens with type aliases export type MyTypeAlias = .... as well, not just interfaces.
I used to just be able to ignore this warning, but now after upgrading to @angular/cli 1.0.1 it will give an error in the browser saying "Failed to compile" along with the error about my interface not being found in the source file. Why is a warning stopping it from running?
In @angular/cli: 1.0.1 I have not warning, but error:

Should I put every single interface or type in a separate file? Even very small? For example:
export type SelectorDataMapperFn<T> = (term?: string) => Observable<T[]>;
export type SelectorListItemMapperFn<T> = (value: T) => SelectorSelectItem<T>;
export type SelectorCompareWithFn<T> = (value1: T, value2: T) => boolean;
export type SelectorCreateNewItemFn<T> = (value: string) => void;
In separate file each? Is it all right?
@manofearth At the moment it is the only possibility to get around the error.
Yeah, updating from 1.0.0 to 1.0.1 prevents the UI from loading at all now. The errors displayed (like in @manofearth 's picture) are still displayed in the console logs as "WARNING"s though - not errors. 1.0.0 doesn't block the UI though.
I found that error disappears if I switch imports from import { X } from './x' to import * as ModuleX from './x'. But it's not very convinient to prepend all X usages in code with module alias: ModuleX.X.
Hi, ` // test.ts export interface test {}
// app.ts import { test } from './test' ` I don't get any warning but the import resulting undefined
same issue
If you put all the interfaces alone in a single type (say a types.ts file) and then import them from there, you should get no errors.
The problem happens when there is some non-type-related code in a file mixed with some types and interfaces. In that case, webpack tries to import a file and look for a symbol in it but it doesn't exist. If the interfaces are all in a separate file, they are only used by TS for type checking and webpack doesn't care about it.
Hmm, I did separate it; yes no error, but by the time i import the file, i could not reference the interface which i exported.. It's declared as undefined thus produce errors at later codes; am I missing something? On Thu, Apr 27, 2017 at 6:39 PM Hugo Campos [email protected] wrote:
If you put all the interfaces alone in a single type (say a types.ts file) and then import them from there, you should get no errors.
The problem happens when there is some non-type-related code in a file mixed with some types and interfaces. In that case, webpack tries to import a file and look for a symbol in it but it doesn't exist. If the interfaces are all in a separate file, they are only used by TS for type checking and webpack doesn't care about it.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/angular/angular-cli/issues/2034#issuecomment-297689689, or mute the thread https://github.com/notifications/unsubscribe-auth/ACmP2QobYmOgxcKUOLSh9fbt6xgsdfljks5r0H5mgaJpZM4J5QId .
thanks but can't we fix it, i know it works with workaround but can't move all my interfaces in each module to one file. need fix
@hccampos
@istiti to be honest, I don't see this getting fixed soon. It is not really an Angular CLI problem, and it is not really a Webpack problem. It is not a Typescript problem either... it is sort of in-between everything, and no one party can easily fix it, I think.
I'm curious if the CLI is using Rollup. I've experienced the same kind of issue on an Ionic 2+ project when I used Rollup instead of Webpack to bundle. So if the CLI uses Rollup, I think it might be where the source of this issue resides.
Just FYI for everyone here, the warnings don't block the UI on 1.0.2 or 1.1.0-beta.0, so you can upgrade past 1.0.1 if you need to.
Hi everyone, I'm having the same issue. I was following angular's styleguide by using classes instead of interfaces for type definitions, but decided to refactor and use interfaces instead and discovered the warning message.
However, now that I see that this hasn't been solved yet, I changed back to classes.
My question is, why use interfaces instead of classes? What's the real difference?
Why? A class can act as an interface (use implements instead of extends).
The difference between a class and an interface is this. The class says something about how the thing is constructed. What kind of code it has running behind it. Which may contain methods and so on. The interface only talks about the shape of data. So in terms of communicating to fellow developers the meaning of the code, and interface is the most clear way to talk about data your application does not construct and knows nothing about the construction of. For example, some data arriving over the wire as JSON then deserialized - it won't ever really be an instance of any class other than Object. But it may structurally match an interface.
TypeScript has an interesting "hack" around classes. If you define the class which only has fields, you can then treat it much as an interface. It will even pretend that an object is an instance of a class of which it is clearly not, the class only has fields (and therefore works like an interface). I think that is a horrible idea, I don't think it is a good feature, and should be removed. But no one asked me :-)
The difference becomes stark as soon as there is a method added. Once a class or interface has even a single method, the ability to pretend they are the same evaporates.
Agreed @kylecordes, this statement is probably misusing context: "I was following angular's styleguide by using classes instead of interfaces for type definitions".
A class and an interface serve two very different purposes. One is about concrete implementation, the other abstracted behavior. I'm confident the Angular style guide would not equate the two as interchangeable.
@Snesi , the difference is, a class is a concrete thing. For example, "Car" is not a behavior, or (as Scala calls them, which to me makes a lot more sense than "interface"), a "trait". Car IS Car. But, "Car" can be "Drivable". That's a trait that can be shared across different kinds of things; a Bike, a power mower, etc. If you were to say, "Oh I just create ICar, IBike, IPowerMower"...that's misuse, which always leads to your question, "why bother with interfaces?" Better to do something like "Vehicle" (subclass Car, Bike, PowerMower), with traits IDriveable, IFuelable, etc. This way, you can even have a vehicle, say, that isn't drivable, you would create "IRemotelyOperated" or some such. ISelfDriveable extends IDriveable. You get the idea. Now you can have a Car that can be operated a variety of ways, specified by different interfaces.
But if you just create an interface that does nothing every time but mirror the entire implementation of a specific concrete thing, you'll find you end up with a lot more interfaces than you should, because there's no behavioral abstraction, and interface will just appear superfluous.
@tcoz @kylecordes Thank you, I understand the difference in languages like Java or PHP, but if Typescript allows to use a class like if it's an interface, why not embrace that hack?
Also, you are right @tcoz. I misinterpreted the angular guideline. This is what it says about interfaces:
Do name an interface using upper camel case.
Consider naming an interface without an I prefix.
Consider using a class instead of an interface.
Why? TypeScript guidelines discourage the I prefix.
Why? A class alone is less code than a class-plus-interface.
Why? A class can act as an interface (use implements instead of extends).
Why? An interface-class can be a provider lookup token in Angular dependency injection.
It just says to consider using a class instead of an interface because it can be implemented instead of extended and because it can be a provider lookup token for dependency injection in Angular (I have no idea what that is)