Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
Is there a way to find only unused JSDoc type definitions?

For example:

/**
 * A number, or a string containing a number.
 * @typedef {(number|string)} NumberLike
 *
 * Dummy type
 * @typedef {(string|null)} StringLike
 */

/**
 * Set the magic number.
 * @param {NumberLike} x - The magic number.
 */
function setMagicNumber(x) {
}

As you can see NumberLike is used, but the StringLike type is not used and I'd like to find all such unused type definitions on our project. It's a Nodejs project with typescript installed.

Here's our tsconfig.json file:

{
  "compilerOptions": {
    "baseUrl": ".",
    "jsx": "react",
    "allowJs": true,
    "checkJs": true,
    "target": "ESNext",
    "noEmit": true,
    "moduleResolution": "node",
    "isolatedModules": true,
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "strictNullChecks": true,
  },
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I'm not presently familiar with AST tools (which I think would be the domain of tools for finding this information). However, here's an example to illustrate an idea:

Let's say that the example JavaScript file in your post is saved at ./module.mjs. Here's another module, ./find-typedefs.mjs:

import {promises as fs} from 'fs';

function getTypedefNames (sourceText) {
  const regex = /@typedef\s+{[^}]+}\s+[A-Za-z]+/g;
  return [...new Set((sourceText.match(regex) ?? [])
    .map(str => str.split(/\s+/).slice(-1)[0]))];
}

async function main () {
  const [filePath] = process.argv.slice(2)
  const text = await fs.readFile(filePath, {encoding: 'utf8'});
  const names = getTypedefNames(text);
  console.log(names.join('\n'));
}

main();

Running the file outputs this:

$ node find-typedefs.mjs module.mjs
NumberLike
StringLike

By using the function getTypedefNames as a basis for potentially more abstractions, you could automate searching the files in your project to produce a list of names that you can then search (and, for example, count) in each file. If you are using an editor like VS Code, you can also use regular expressions (like the one in that function) directly in the editor's Find menu.

Hopefully this will save you some manual work of eye-scanning.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!