Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WARNING: '__read' is imported from external module 'tslib' but never used #1543

Closed
cacothi opened this issue Feb 13, 2020 · 14 comments
Closed

Comments

@cacothi
Copy link

cacothi commented Feb 13, 2020

Type of Issue

[ X] Bug Report
[ ] Feature Request

Description

Trying to build the package and getting the following error warning

WARNING: '__read' is imported from external module 'tslib' but never used

Not usre where it's been exported, but it's not from my project.

Note, I've just update from Angular 8 to 9.0.1

How To Reproduce

ng build

Expected Behaviour

Success with no warning

Version Information

$ node_modules/.bin/ng-packagr --version
ng-packagr: x.y.z
@angular/*: x.y.z
typescript: x.y.z
rxjs: x.y.z
node: x.y.z
npm/yarn: x.y.z

Please include all version numbers that might be relevant, e.g. third-party libraries

@sahilpurav
Copy link

I'm facing the exact same issue. It would be great if someone can get along and find the root cause or the place where the library is imported incorrectly. My application works great though!

@kgajera
Copy link

kgajera commented Feb 22, 2020

I was getting the same error. To find where it was coming from, I searched the dist directory for __read. I found that it was being included because my module was using the spread operator:

@NgModule({
  declarations: [
    ...COMPONENTS
  ],
)}

@cacothi
Copy link
Author

cacothi commented Feb 23, 2020 via email

@multimike77
Copy link

multimike77 commented Mar 3, 2020

Something which is probably related to this problem as well.
On my library project I was getting many errors like
WARNING: 'OnInit', 'OnDestroy', 'AfterContentInit' are imported from external module '@angular/core' but never used
apart from having the __read is imported... erorr as described above as well.

After experimenting a bit, I found out something interesting. I could manage to make the warning about OnInit, OnDestroy, etc. disappear by separating the import statements in two:

  • one which only contains imports relevant for type declarations
  • all other imports, which are also used for values etc.

For example:

import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core';

@Component({
  selector: 'lib-my-lib',
  template: `
    <p>
      my-lib works!
    </p>
  `,
  styles: [],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class MyLibComponent implements OnInit, OnDestroy {

  constructor(
      private cd: ChangeDetectorRef
  ) { }

  ngOnInit(): void {
    this.cd.markForCheck();
  }

  ngOnDestroy(): void {
  }

}

this compiles with warning
WARNING: 'OnDestroy' and 'OnInit' are imported from external module '@angular/core' but never used.
But when changing the imports to be like

import { ChangeDetectionStrategy, Component, OnDestroy, OnInit } from '@angular/core';
import { ChangeDetectorRef } from '@angular/core';

compilation works fine without any warnings.

So somehow the compiler is no able to optimize/rewrite the import statements so that only relevant imports are kept.

Probably related to: angular/angular#21280

@vltansky
Copy link

Same here

@rafa-suagu
Copy link

Some news here?

@Jack-WangZhe
Copy link

Thanks for @multimike77 's answer, but is there anyone who know why such the change will take effect?
Thanks!
Jack

@pjir
Copy link

pjir commented May 21, 2020

Hey @multimike77,

Classes have a value at runtime and a type at design-time, and the use is context-sensitive.
From typescript 3.8 you should use:
import type { ...
Import type only imports declarations to be used for type annotations and declarations. It always gets fully erased, so there’s no remnant of it at runtime.

You can't mix them in one import line.

@Bil0
Copy link

Bil0 commented May 28, 2020

Hello,
I'm getting the same WARNING by using the spead operator in a component from my lib.

The __read is imported from tslib in the compiled file but unused
import { __decorate, __read, __spread } from "tslib";

@harunergul
Copy link

harunergul commented Jun 3, 2020

In my case tree dots(...) inside one of my component cause the same error.

ngOnInit(): void { this.selectedPatient = [ ... [this.patients[0]]]; }

when ever i remove these tree dots the warning goes away.
Current warning is WARNING: '__read' is imported from external module 'tslib' but never used

any improvements?

evolkmann added a commit to exportarts/ngx-prismic that referenced this issue Jul 1, 2020
hakimio added a commit to hakimio/ngx-tour that referenced this issue Aug 10, 2020
hakimio added a commit to hakimio/ngx-tour that referenced this issue Aug 10, 2020
… from external module '@angular/core' but never used" Angular warning by separating type definition imports. See ng-packagr/ng-packagr/issues/1543#issuecomment-593873874 for more info.
@Dzivo
Copy link

Dzivo commented Sep 2, 2020

I have same warning in angular library any news on this. Is this something to be worried about

@SchnWalter
Copy link
Contributor

I wasn't able to reproduce the issue. The spread operator inside the NgModule declarations property doesn't cause any issues. @Dzivo, can you please provide a minimal repository where this issue is present?

NOTE: In this issue people talk about two different warnings with a similar message:

  1. There's the warning reported in the title of this issue, for which a reproduction could be useful.
  2. The warnings related to type-only imports which are related to a change in TypeScript and has nothing to do with ng-packagr, and you can find those warnings all over the place, in various projects.

@alan-agius4
Copy link
Member

I am unable to reproduce. Please file a new issue with a reproduction if the problem persists.

@github-actions
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

This action has been performed automatically by a bot.

@github-actions github-actions bot locked and limited conversation to collaborators Nov 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests