12

There are tslint rules available for private method. But How can I find out if the public method is not used anywhere in the project and hence candidate for the dead code. I am using Visual studio code for Typescript.

Wickramaranga
  • 943
  • 2
  • 19
  • 35
Pramod
  • 191
  • 2
  • 2
  • 6
  • I doubt you will find a TSLint rule that can do this, as TSLint processes the files individually. To write a rule that deals with multiple files, it's necessary to analyse the compiled program in a different manner. Private methods can only reside in the one file, that's why those are easily checked. – cartant May 05 '18 at 00:38

4 Answers4

6

Have a look to https://www.npmjs.com/package/ts-unused-exports

It produces an output like this

$ ts-unused-exports tsconfig.json

4 modules with unused exports

src/lambda: handler
src/routes/crud: default
bitIO
  • 384
  • 3
  • 11
  • 2
    The question is about unused code, not just unused exports. – C4d Jan 14 '20 at 13:16
  • 2
    By detecting unused exports you can find dead code. But if you have a solution, please, share – bitIO Jan 29 '20 at 10:29
  • He is asking for detection of unused public functions. What if you have an angular component? You can have an unexported public function that gets called from the html-template. Its not exported, yet still in use. Exports only wont do it. – C4d Jan 29 '20 at 10:59
  • 1
    Also try `ts-prune` package which does the same. – dinesh ygv Jun 04 '20 at 10:31
1

I had the same problem, and dpdm was the best found solution. But I wanted one integrated in Visual Code so in the end was created an extension named Find unused exports. It allows to easily see and go to unused exports in a js/ts project.

1

It's a quite complex issue with VSCode for now. Suggest you switch to WebStorm https://dev.to/mokkapps/why-i-switched-from-visual-studio-code-to-jetbrains-webstorm-939

There is a lot of complaint about it on this thread https://github.com/microsoft/TypeScript/issues/29293 but unfortunately it never gets resolved.

Vincent
  • 1,178
  • 1
  • 12
  • 25
0

Well, I don't think you can do that for all your code, but you can open the context menu over a method/function/property, and then select 'find all references'. If none is found, you can mark it as a candidate for deletion.

Of course this method is not perfect. For example, you could call a method from an object which is an instance of a class but has been cast to any previously, but it's a starting point.

Oscar Paz
  • 18,084
  • 3
  • 27
  • 42