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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:16
  • Comments:40 (6 by maintainers)

github_iconTop GitHub Comments

23reactions
Draccozcommented, Mar 13, 2017

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.

17reactions
filipesilvacommented, Mar 13, 2017

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript, index.d.ts and webpack: Module not found error
Came up with a solution to use index.ts for all exports instead of index.d.ts. ... I had to change types to types.d only...
Read more >
Documentation - Modules .d.ts - TypeScript
Comparing JavaScript to an example DTS. Common CommonJS Patterns. A module using CommonJS patterns uses module.exports to describe the exported values.
Read more >
Module Resolution - TypeScript
Module resolution is the process the compiler uses to figure out what an import refers to. Consider an import statement like import {...
Read more >
Module defined in typings (d.ts files) can't be resolved if there ...
Kotlin detected. Foo is not resolved; the error goes away if I comment out the import in d.ts file. The issue can be...
Read more >
Could not find declaration file for module 'X' Error | bobbyhadz
The error "Could not find declaration file for module" occurs when TypeScript cannot find the type declaration for a module. To solve the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found