question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

False positives on types imported with `import type`

See original GitHub issue

It’s possible that this is an issue with ts-morph, but wanted to report this here. The new import type syntax introduced in TypeScript 3.8 appears to be resulting in false positives. Here’s an example (applying this patch on this repo @ https://github.com/nadeesha/ts-prune/commit/e879ac6e4d29e64fea872205a8d7c37e19256b22):

diff --git a/src/analyzer.ts b/src/analyzer.ts
index d767698..726f3ba 100644
--- a/src/analyzer.ts
+++ b/src/analyzer.ts
@@ -8,6 +8,7 @@ import {
 } from "ts-morph";
 import { isDefinitelyUsedImport } from "./util/isDefinitelyUsedImport";
 import { getModuleSourceFile } from "./util/getModuleSourceFile";
+import type { SomeType } from "./presenter";
 
 type OnResultType = (result: IAnalysedResult) => void;
 
@@ -22,7 +23,7 @@ export interface IAnalysedResult {
   symbols: Array<string>;
 }
 
-function handleExportDeclaration(node: SourceFileReferencingNodes) {
+function handleExportDeclaration(node: SourceFileReferencingNodes, _someParam?: SomeType) {
   return (node as ExportDeclaration).getNamedExports().map(n => n.getName());
 }
 
diff --git a/src/presenter.ts b/src/presenter.ts
index dcf1320..2115e1e 100644
--- a/src/presenter.ts
+++ b/src/presenter.ts
@@ -15,3 +15,5 @@ export const present = (state: State) => {
 
   return [].concat.apply([], unused2D) as Array<IAnalysedResult>;
 };
+
+export type SomeType = any;

This results in:

 ➜  ts-prune git:(master) ✗ ts-prune
SomeType @ ./src/presenter.ts

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
nadeeshacommented, May 28, 2020

I couldn’t reproduce this is node 12 btw. Maybe something else is at play here. Anyways, thanks for reporting!

1reaction
JustFly1984commented, May 29, 2020

@nadeesha import type is supported by @babel/core 6.9.0 and newer current version is 7.1.1

Read more comments on GitHub >

github_iconTop Results From Across the Web

[no-duplicate-imports] false positive rule on "import type" #2315
Repro import type { Foo } from './foo' import { foo } from './foo' reports eslint(no-duplicated-imports) error.
Read more >
JavaScript/TypeScript false positive with absolute imports
Importing a module that is not declared as a dependency makes it an implicit one and is bound to create problems.
Read more >
Typescript + Eslint false positive for imports + `declare global`
1 Answer 1 ... Turn off "no-undef": "off"(disallow the use of undeclared variables unless mentioned in /*global */ comments) and to be handled...
Read more >
Common issues and solutions - mypy 0.991 documentation
ImportError from circular imports. NameError: name "X" is not defined from forward references. TypeError: 'type' object is not subscriptable from types that ...
Read more >
Invalid 'unused import' highlighting in TypeScript when using ...
... compiler needs to generate the type declarations, so needs the types it uses to be imported. This can lead to false positives...
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