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.
I would say for the very reason you suggest: it's a hack. I'm not sure language is a determining factor. An interface is an interface.
Now, if TypeScript has underlying technical reasons for not using interfaces the way everything else does because the language itself can't support it properly, that's a different matter. But they should make it clear that this is the case. And increasingly, it does seem interfaces can be problematic in TS.
Also the whole "without the I" thing is dubious. The C# style guide, very first example of Interface, shows:
interface IEquatable
(Note also the name implies a behavior, not a concrete "thing").
So it seems contradictory to me that TS would say "no" but C# clearly says "yes", unless the point was to be able to tell a C# interface from a TS one...which again, would be a questionable practice IMHO. Or, again, unless the reason was the underlying technology itself.
Also, say you're iterating a collection and filtering on some types. Are they interfaces, or class types? How do you know just by looking at the code? In a review (like a GitHub side-by-side), where's there's no hover tooling and so on, that could be very annoying.
Why depart from such a useful standard? Myself, that's one TS thing (if it is in fact a formal recommendation) I might discard.
I've had a read of the styleguide, and I think it's simply poorly worded. What I believe it means is instead of doing this:
interface IItemService {}
class ItemService implements IItemService {}
class OtherService {
constructor(itemService: IItemService) {}
}
to do this:
class ItemService {}
class OtherService {
constructor(itemService: ItemService) {}
}
This is because in the latter case, the class itself can be injected by-reference (this is what the "provider lookup token" bit means - the class constructor is used as the thing to identify what we're trying to inject here.) The style guide is simply advising against a pattern that is quite common e.g. in Java with Spring, where every dependency should be an interface and then the DI framework figures out which implementation to use. It states that in Typescript that creates unnecessary overhead, and in 99% of cases your interface would only have one implementation anyway so it's unnecessary to prematurely optimise this by leveraging polymorphism.
I don't believe the style guide is actually stating "use classes instead of interfaces always" - I think it's stating "use just classes instead of class-plus-interface for injectable classes."
Regardless, I believe this is quite off-topic and is just making a long thread longer, so we should probably stop discussing the style guide.
@tcoz
Also the whole "without the I" thing is dubious. The C# style guide, very first example of Interface, shows:
interface IEquatable
(Note also the name implies a behavior, not a concrete "thing").
If you go by that logic, You don't need to pretend I to Equatable to know that its a behaviour. The able sufix of Equatable clearly implies this is a interface.
So I is completely redundant in this example.
Some pretty easy examples where that doesn't work. Example: "IComponent", "IProvider". That would have to be "Compentable", "Providerable". That'd just be odd. Those btw are again from C#.
I really can't see a valid reason to make TS different from everything else on earth that way, particularly it's closest analog, C#.
I'm sorry to have brought this up, but continuing this discussion about code style is completely off-topic xD.
For now, just so I don't see the warning export 'TestInterface' was not found in './test.service' I preferred to use classes instead of Interfaces, because I don't want to split every Interface into separate files.
And thanks to typescript's "hack" that allows you to use classes like if they were interfaces, whenever this bug is fixed, I could change the classes to interfaces and everything should continue working without issues.
Just to obscure this weird error a bit more... I have the same warning for:
// Warning: "export 'BillState' was not found in '../../state/reducers/bills'
@Input() bill: BillModel;
However, if I change the type to be an array of BillModel the error is gone.... :sheep:
// No warning :-)
@Input() bill: BillModel[];
@gionkunz I have the same behavior as you, except that it's crashing my entire site. It's only in certain files, when I import the interface and assign it as a type, I get the error datatypes_1 is not defined. datatypes is the folder that my interface sits in. It does not matter if I import the interface from the index file in datatypes or straight from the file where it is exported. But, if I cast the object as an array, I can build the project.
I'm experiencing the same issue when referencing interfaces from third-party modules.
Heya all, I've been looking at this problem and wanted to give you an update.
There's two parts to the issue:
- interfaces don't really exist after TS compilation.
- Webpack tries to follow imports on ES modules.
So for things that need to exist and be imported after compilation, like a decorated type (e.g. @Input() test: TestInterface), you will get a warning that the import cannot be found by webpack. It simply does not exist anymore after compilation (AOT does not suffer from this problem btw).
A couple of workarounds have been proposed, like keeping files that only have interfaces, or declaring the interface in the same class that uses it. These address the problem in different ways.
When you have a file composed only of interfaces, it is empty after compilation. So Webpack thinks it's a CommonJS module (instead of a ES module) and doesn't try to figure out it's exports, assuming whatever we're importing is there at runtime. See https://github.com/webpack/webpack/issues/2977#issuecomment-245898520 for more details.
Declaring the interface in the same module where you use it means there's no export to follow, so no warning.
I checked with the TypeScript folks (@mhegazy, @DanielRosenwasser) and @alexeagle, and this is indeed an unfortunate situation for which there is no clear fix.
The recommendation is to use a value that exists after compilation for decorated properties. For instance, a class or variable instead of an interface.
I know this is frustrating because you are encouraged to fully make use of TypeScript, but it is a current limitation.
I'll communicate this to docs to make sure guidance is provided.
Thanks to all that have been following this, providing context, reproductions and workarounds. I'm sorry I don't have a real fix that just makes it go away.
+1
@filipesilva So the best practice is to use class instead?
@wilsoncook yes.
I just got this warning for the first time today, apparently because I had a file that exported a const as well as an interface. The weird thing is I had already been using the interface elsewhere (within Providers and within other models) for a while without problems. The warning didn't crop up until I tried to use the interface from a component.
The workaround was to move the exported constant out of that file. It seems as long as individual TS files contain only compile-time constructs (e.g. types, interfaces) OR runtime constructs (classes, consts), but not mixed, then the warning will not occur.
IMHO, this seems like a much better approach than telling people to switch to using classes everywhere, because while TypeScript might treat class type annotations the same way as interface type annotations, the semantics of classes in JavaScript itself are quite different. Classes in JavaScript > ES2015 mean you're expecting certain methods and you're expecting a certain prototype and a certain constructor with possibly certain static fields on it, and you're expecting to be able to "instanceof" it and always get true if it matches. To abuse classes to get type-safety on plain JS data objects is not the right way to go, IMHO.
@mischkl same concern
So the best practice is to use class instead?
@filipesilva But as I understand it adds extra unused source code to the resulting bundle or I miss something?
IMHO, this seems like a much better approach than telling people to switching to using classes everywhere
@mischkl ~~The thing is you can't completely split interfaces and the real code in some situations. Here is one of them:~~
import {SomeComponent} from './some.component';
export interface SomeInterface {
ref: ComponentRef<SomeComponent>;
}
~~In this case you have to import the real component to describe an interface and resulting file won't be empty after compilation.~~
@th0r the example you mention should not be a problem. The problem only comes when you export a mixture of compile-time and run-time stuff in one file, e.g.:
export class SomeClass {
foo: string;
}
export interface SomeInterface {
bar: string;
}
@mischkl ~~I mean your solution (extracting all compile-time stuff into a separate file) won't work for my example: I extracted SomeInterface but it depends on run-time code (SomeComponent) and resulting file won't be empty after compilation.~~
@th0r I guess I didn't specify what I meant exactly enough. You can mix and match compile-time stuff with non-compile-time stuff as much as you want AFAIK, the only problems come when you export both from one file.
So the solution is to separate out all interface and type exports into extra files, which can then be imported elsewhere and used freely.
So the solution is to separate out all interface and type exports into extra files, which can then be imported elsewhere and used freely.
~~This won't work for my example because of this (quote from this comment):~~
When you have a file composed only of interfaces, it is empty after compilation. So Webpack thinks it's a CommonJS module (instead of a ES module) and doesn't try to figure out it's exports, assuming whatever we're importing is there at runtime.
~~In my example this file with interfaces won't be empty because you imported SomeComponent into it that is needed to describe SomeInterface.~~