What is detective-typescript?
The detective-typescript npm package is designed to analyze TypeScript files to extract dependencies, such as import and require statements. It can be used to understand the structure of a TypeScript project, identify unused dependencies, or automate tasks that involve analyzing the dependency graph of a project.
What are detective-typescript's main functionalities?
Extract import statements
This feature allows you to extract all the import statements from a given TypeScript file content. The code sample demonstrates how to use detective-typescript to parse a string containing TypeScript code and return an array of the imported modules.
const detective = require('detective-typescript');
const content = `import { join } from 'path';
import * as fs from 'fs';`;
const dependencies = detective(content);
Extract require statements
In addition to import statements, detective-typescript can also extract require statements from TypeScript files. This is useful for projects that use a mix of ES6 and CommonJS modules. The code sample shows how to extract dependencies from a string of TypeScript code that uses require.
const detective = require('detective-typescript');
const content = `const express = require('express');
const app = express();`;
const dependencies = detective(content);
Other packages similar to detective-typescript
precinct
Precinct is a more general-purpose dependency detection tool that supports multiple file types, including JavaScript, TypeScript, and CSS. While detective-typescript focuses specifically on TypeScript, Precinct provides a broader range of functionality but might be less specialized for TypeScript-specific nuances.
madge
Madge is a tool for generating a visual graph of module dependencies, and it can analyze TypeScript files among others. Unlike detective-typescript, which is focused on extracting dependencies from file content, Madge provides a higher-level overview of the project structure and can be used for visualizing and analyzing the dependency graph.
detective-typescript
Get the dependencies of TypeScript module
npm install detective-typescript
Usage
var detective = require('detective-typescript');
var mySourceCode = fs.readFileSync('myfile.js', 'utf8');
var dependencies = detective(mySourceCode);
Options
skipTypeImports
(default: false) - Skips imports that only imports typesmixedImports
: (default: false) - Include CJS imports in dependency list
License
MIT