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.
-
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 Answers
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

- 384
- 3
- 11
-
2
-
2By 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
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.

- 31
- 3
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.

- 1,178
- 1
- 12
- 25
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.

- 18,084
- 3
- 27
- 42