This document explains the TypeScript conversion of the decision-tree module and how to use it.
The module has been converted from JavaScript to TypeScript while maintaining 100% backward compatibility. Existing JavaScript projects will continue to work without any changes.
- Complete type definitions for all methods and properties
- Interface definitions for training data and model structures
- Compile-time type checking for better development experience
- Source maps for debugging
- Declaration files (
.d.ts) for IDE support - Better IntelliSense and autocomplete
- TypeScript compiler with ES5 output for maximum compatibility
- Watch mode for development
- Clean build process
├── src/ # TypeScript source files
│ └── decision-tree.ts # Main implementation
├── lib/ # Compiled JavaScript (generated)
│ ├── decision-tree.js # Main module
│ ├── decision-tree.d.ts # Type definitions
│ └── *.map # Source maps
├── examples/ # Usage examples
│ ├── typescript-usage.ts
│ └── javascript-usage.js
└── tst/ # TypeScript test files
├── decision-tree.ts
├── evaluation.ts
└── reported-bugs.ts
import DecisionTree from 'decision-tree';
interface TrainingData {
color: string;
shape: string;
liked: boolean;
}
const trainingData: TrainingData[] = [
{ color: "blue", shape: "square", liked: false },
{ color: "red", shape: "circle", liked: true }
];
const dt = new DecisionTree('liked', ['color', 'shape']);
dt.train(trainingData);
const prediction = dt.predict({ color: "blue", shape: "hexagon" });const DecisionTree = require('decision-tree');
const trainingData = [
{ color: "blue", shape: "square", liked: false },
{ color: "red", shape: "circle", liked: true }
];
const dt = new DecisionTree('liked', ['color', 'shape']);
dt.train(trainingData);
const prediction = dt.predict({ color: "blue", shape: "hexagon" });import DecisionTree from 'decision-tree';
const dt = new DecisionTree('liked', ['color', 'shape']);
// ... rest of the code# Install dependencies
npm install
# Build the project
npm run build
# Watch mode for development
npm run build:watch
# Run tests
npm test
# Run examples
npm run example:js # JavaScript example
npm run example:ts # TypeScript example
# Clean build artifacts
npm run cleanThe module provides comprehensive TypeScript interfaces:
interface TreeNode {
type: string;
name: string;
alias: string;
val?: any;
gain?: number;
sampleSize?: number;
vals?: TreeNode[];
child?: TreeNode;
prob?: number;
}
interface DecisionTreeData {
model: TreeNode;
data: any[];
target: string;
features: string[];
}If you're using the module in JavaScript, no changes are needed. The compiled JavaScript maintains the exact same API.
To add TypeScript support to your project:
- Install the module:
npm install decision-tree - Import with types:
import DecisionTree from 'decision-tree' - Define interfaces for your data structures
- Enjoy full type safety!
The compiled JavaScript is ES2022 compatible and works in:
- All modern browsers
- Node.js 20+
- Modern bundlers (Webpack, Rollup, Vite, etc.)
- ES module environments
- Zero runtime overhead - TypeScript types are removed during compilation
- Same performance as the original JavaScript version
- Smaller bundle size when using modern bundlers (ES modules)
- Modern ES2022 features for better performance and smaller code
When contributing to the project:
- Make changes in the
src/directory - Run
npm run buildto compile - Ensure tests pass with
npm test - The compiled JavaScript in
lib/is automatically generated
- Ensure you're importing from the correct path
- Check that your data structures match the expected interfaces
- Use type assertions if needed:
data as TrainingData[]
- Run
npm run cleanto remove old build artifacts - Ensure TypeScript is installed:
npm install typescript - Check
tsconfig.jsonfor configuration issues
- The compiled JavaScript is identical to the original
- Check that you're using the correct import/require syntax
- Verify your data format matches the expected structure
For issues or questions:
- Check the existing test files in
tst/ - Review the examples in
examples/ - Open an issue on GitHub
- Check the main README.md for usage documentation