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

Module not found when using type from .d.ts file

Open Buslowicz opened this issue 9 years ago • 40 comments

I started this issue on angular/angular issues, but seems to be related to CLI, so just copy-pasting the issue here :).

I'm submitting a ...

[x] bug report

Current behavior When assigning a custom type (like Post, or User) to a decorated property (with either ng decorator like @Input() or a custom one), where type is declared in d.ts file, angular throws an error:

ERROR in ./src/app/app.component.ts
Module not found: Error: Can't resolve './types' in '/home/dracco/projects/ng2/src/app'
 @ ./src/app/app.component.ts 11:0-31
 @ ./src/app/app.module.ts
 @ ./src/main.ts
 @ multi webpack-dev-server/client?http://localhost:4200/ ./src/main.ts
webpack: Failed to compile.

This does not happen if:

  • type is added through generic (like Array<Post>) OR
  • no decorator is added to property OR
  • file with declaration has .ts extension instead of .d.ts

Expected behavior No error should be thrown. property should have given type.

Minimal reproduction of the problem with instructions

  1. Run ng init to create a new Angular project.
  2. Create a d.ts file in app folder
  3. Add an exported interface to the .d.ts file
  4. Import above interface in component file
  5. Add a property with decorator and above type (e.g. @Input() prop: MyType)
  6. Run ng serve

What is the motivation / use case for changing the behavior? The default workflow in TypeScript suggests keeping declarations and interfaces in .d.ts file. Simple Angular components might not use Observables and still accept data from the host. I am actually surprised nobody mentioned and solved it so far as it is pretty severe (it also happens in 4.0.0-beta.8).

Please tell us about your environment:

  • OS: linux x64 (Ubuntu 16.10)
  • Angular version: 2.3.1, 2.2.4, 4.0.0-beta.8
  • Angular CLI version: 1.0.0-beta.28.3
  • Node (for AoT issues): 7.5.0
  • NPM: 4.1.2

Buslowicz avatar Feb 21 '17 14:02 Buslowicz

Possibly has to do with this issue.

nikoTM avatar Feb 21 '17 15:02 nikoTM

Maybe, though I do refer to d.ts file without extension in the import statement.

Buslowicz avatar Feb 21 '17 15:02 Buslowicz

Why not import the .d.ts into the typings.d.ts file instead of into the component?

aaron-bond avatar Feb 22 '17 11:02 aaron-bond

Because it's generated and exports interfaces and namespaces, not declares them globally.

Buslowicz avatar Feb 22 '17 11:02 Buslowicz

This isn't a CLI issue, but rather a TypeScript/Angular issue.

filipesilva avatar Mar 13 '17 18:03 filipesilva

They said it's cli issue. Is Angular and CLI 2 separate products? If not, do cooperate. This issue does not happen if using webpack starter, so I don't believe it's issue on Angular or TypeScript side itself. Please reopen this issue and investigate it if you want people to believe that Angular is a serious platform.

Buslowicz avatar Mar 13 '17 18:03 Buslowicz

I can confirm this issue, @Draccoz @filipesilva .

It does not happen when you change the filename from .d.ts to .ts. It also does not happen when you change your import from import { Something } from 'yourlib'; to import { Something } from 'yourlib.d';

I just moved a working project from a webpack-starter seed (TypeScript/Angular) to CLI and got this error, so I assume it has something to do with the generated CLI project.

I'm using @angular/cli: 1.0.0-rc.2

basst314 avatar Mar 17 '17 13:03 basst314

Thanks @basst314. I'm afraid though that @filipesilva doesn't care about it at all.

Buslowicz avatar Mar 17 '17 13:03 Buslowicz

Hi @filipesilva , how are we supposed to handle custom typing files like my-types.d.ts in projects running on angular-cli when we want to use them in our component inputs as @Input() element: MyType ?

As written above, regular typescript imports like import { MyType } from 'my-types' don't really work when running on a cli-project.

Do we have to register our typing-files somewhere in the cli-config to be found during the build? Or is there a documentation or guide you can refer us to?

Any help is very much appreciated! Thanks!

basst314 avatar Mar 17 '17 15:03 basst314

This might actually be https://github.com/angular/angular-cli/issues/2034.

filipesilva avatar Mar 18 '17 17:03 filipesilva

Hey @Draccoz, can you confirm that this does only happen when doing a normal jit build with ng build, but does work like a charm when building aot with ng build -aot? This is what I'm experiencing.

@filipesilva, #2034 might be related, but this one breaks the build, the other displays a warning and does not break the build. Also the concrete error message is different. Could you please consider reopening this issue and take a look at @Draccoz github repo to reproduce the error?

Thanks!

basst314 avatar Mar 22 '17 12:03 basst314

Confirming the above. ng build -aot works correctly, while ng build produces:

ERROR in ./src/app/app.component.ts Module not found: Error: Can't resolve './types' in '/home/dracco/projects/ng2-type-import-issue/src/app' @ ./src/app/app.component.ts 11:0-31 @ ./src/app/app.module.ts @ ./src/main.ts @ multi ./src/main.ts

Buslowicz avatar Mar 22 '17 12:03 Buslowicz

Can confirm this is still an issue in Angular CLI 1.0.2

@angular/cli: 1.0.2 node: 7.10.0 os: win32 x64 @angular/animations: 4.1.1 @angular/common: 4.1.1 @angular/compiler: 4.1.1 @angular/core: 4.1.1 @angular/forms: 4.1.1 @angular/http: 4.1.1 @angular/material: 2.0.0-beta.3 @angular/platform-browser: 4.1.1 @angular/platform-browser-dynamic: 4.1.1 @angular/router: 4.1.1 @angular/cli: 1.0.2 @angular/compiler-cli: 4.1.1

18steps avatar May 08 '17 15:05 18steps

@basst314 you're right, #2034 is a warning and this is an error. Reopening.

filipesilva avatar May 09 '17 10:05 filipesilva

any news on this? it's pretty annoying!

artparks avatar Jun 02 '17 13:06 artparks

+1

cipchk avatar Jun 11 '17 11:06 cipchk

+1 Any updates on this?

alexey-kozlenkov avatar Jul 10 '17 23:07 alexey-kozlenkov

Does anyone have a workaround for this (aside from using the any type on the input)?

[edit]: a workaround for anyone following along is to declare the property with an intersection type:

export class FooComponent {
  @Input() public foo: IFoo; // does not work
  @Input() public foo: IFoo & { }; // works
}

marshall007 avatar Jul 20 '17 22:07 marshall007

name the files differently. e.g. .def.ts

On 21 Jul 2017 00:44, "Marshall Cottrell" [email protected] wrote:

Does anyone have a workaround for this (aside from using the any type on the input)?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/angular/angular-cli/issues/4874#issuecomment-316851117, or mute the thread https://github.com/notifications/unsubscribe-auth/ACwLMvfmURHRHyCsREdtbWeRjQF4lkzKks5sP9hPgaJpZM4MHWjJ .

18steps avatar Jul 21 '17 06:07 18steps

@18steps I'm affraid this is not possible in my case.

TypeScript treats *.d.ts files in a special way that it doesn't compile them into the bundle.

In my project (non-angular), I use *.d.ts file which is placed outside the root of my project. The reason for that is because the file is shared with another project (angular + angular/cli). When compiling, TypeScript refers to that file with '../../file_path.d.ts' but keeps the reference to the root path properly. Once I change the file name to *.def.ts it is no longer special for TS and the compiler moves the root path two levels up. This ruins the whole tree of compiled files.

Here's an example how it looks when I use *.d.ts file:

└ my-project/  <-- I run tsc from this location
  └ tsconfig.json
  └ src/
    └ app.ts  <-- (imports something from ../../d/definitions.d.ts)
  └ dist/  <-- this is generated by tsc
    └ app.js
└ d/
  └ definitions.d.ts

And here's how it looks when I change the *.d.ts to *.def.ts (or anything else):

└ my-project/  <-- tsc is run from this place
  └ tsconfig.json
  └ src/
    └ app.ts  <-- (imports something from ../../d/definitions.def.ts)
  └ dist/  <-- this is generated by tsc
    └ my-project  <-- this is wrong
      └ src
        └ app.js
      └ d
        └ definitions.def.ts
└ d/
  └ definitions.def.ts

If you take a look at the /dist folder in the second example you'll notice that the compiler "moved" the root of the application 2 levels up.

I was searching for a solution for this behavior in the TS documentation but didn't find anything useful. Maybe someone knows some trick in the tsconfig.json that would solve my problem but this is something different from the bug described in this thread.

bmxmaga avatar Jul 25 '17 12:07 bmxmaga