Class Methods Inspection Utilities
Powerful utilities for examining and summarizing the instance/static methods of JavaScript/TypeScript classes. Analyze class APIs, count methods, and get comprehensive breakdowns—great for meta-programming, documentation tooling, and runtime validation.
Import
import {
getInstanceMethodNames,
getStaticMethodNames,
countInstanceMethods,
countStaticMethods,
getClassDetails,
} from 'nhb-toolbox';
Quick Overview
- getInstanceMethodNames: Lists names of all instance methods declared directly on a class.
- getStaticMethodNames: Lists static method names declared on the class itself.
- countInstanceMethods/countStaticMethods: Get instance/static method counts.
- getClassDetails: Get comprehensive info (names & counts).
- All return values are sorted alphabetically.
Usage Examples
- Fixture
- Instance Methods
- Static Methods
- Count Instance
- Count Static
- getClassDetails
class Example {
constructor() {}
foo() {}
bar() {}
static zap() {}
static zip() {}
}
getInstanceMethodNames(Example)
// Returns: ['bar', 'foo']
getStaticMethodNames(Example)
// Returns: ['zap', 'zip']
countInstanceMethods(Example)
// Returns: 2
countStaticMethods(Example)
// Returns: 2
getClassDetails(Example)
// Returns:
// {
// instanceNames: ['bar', 'foo'],
// staticNames: ['zap', 'zip'],
// instances: 2,
// statics: 2,
// total: 4
// }
API Reference
getInstanceMethodNames
function getInstanceMethodNames(cls: Constructor): string[]
Returns the sorted names of all instance methods defined directly on the class prototype (excluding inherited members and the constructor).
Parameter | Type | Description |
---|---|---|
cls | Constructor | The class constructor (not an instance) |
getStaticMethodNames
function getStaticMethodNames(cls: Constructor): string[]
Returns the sorted names of all static methods defined directly on the class (excluding 'prototype', 'name', 'length', etc.).
countInstanceMethods
function countInstanceMethods(cls: Constructor): number
Returns the count of instance methods (see getInstanceMethodNames).
Alias
getInstanceMethodsCount
countStaticMethods
function countStaticMethods(cls: Constructor): number
Returns the count of static methods (see getStaticMethodNames).
Alias
getStaticMethodsCount
getClassDetails
function getClassDetails(cls: Constructor): ClassDetails
Returns a clean summary of a class’s instance/static method names and counts.
Returns
interface ClassDetails {
instanceNames: string[]; // List of instance method names
staticNames: string[]; // List of static method names
instances: number; // Number of instance methods
statics: number; // Number of static methods
total: number; // Total methods (instance + static)
}
Key Features
- Alphabetical Sorting: Always get deterministic, sorted lists.
- Directly Defined Only: Ignores prototype-chain/inherited members.
- TypeScript Support: Works seamlessly in TS and JS.
Limitations
- No Inherited Methods: Only methods defined directly on the class (not the parent) are listed.
- No Symbol Support: Only string-named methods are included (symbol-named methods are ignored).
- No Property Types: Does not indicate async/getter/setter/property nature; only whether a key is a method.
Recommended Use Cases
- Building class API documentation tools.
- Validating shape of user-extended classes.
- Automatic test-suite coverage reporting for class APIs.
- Debugging and runtime introspection for advanced libraries.
Conclusion:
These utilities provide developer-friendly introspection of any ES6 class or constructor, helping with reflection, documentation, metaprogramming, and testing. Easy to drop in and reliable for modern JavaScript and TypeScript codebases.