Class and inheritance helpers provide comprehensive support for JavaScript class syntax including constructor validation, inheritance chains, super calls, and property access patterns.
Ensures constructor functions are called with the new keyword.
/**
* Ensures constructor is called with 'new'
* @param instance - The instance being constructed
* @param Constructor - Constructor function
* @throws TypeError if called without 'new'
*/
function classCallCheck(instance: any, Constructor: Function): void;Usage Example:
import _classCallCheck from '@babel/runtime/helpers/esm/classCallCheck';
function MyClass() {
_classCallCheck(this, MyClass);
// constructor body
}Creates a class with property descriptors for methods and static properties.
/**
* Creates class with property descriptors
* @param Constructor - Constructor function
* @param protoProps - Instance method descriptors
* @param staticProps - Static method descriptors
* @returns Enhanced constructor function
*/
function createClass(
Constructor: Function,
protoProps?: PropertyDescriptor[],
staticProps?: PropertyDescriptor[]
): Function;
interface PropertyDescriptor {
key: PropertyKey;
value?: any;
get?: () => any;
set?: (value: any) => void;
kind: 'method' | 'get' | 'set';
static?: boolean;
}Usage Example:
import _createClass from '@babel/runtime/helpers/esm/createClass';
import _classCallCheck from '@babel/runtime/helpers/esm/classCallCheck';
function Person(name) {
_classCallCheck(this, Person);
this.name = name;
}
_createClass(Person, [{
key: 'sayHello',
value: function sayHello() {
return `Hello, I'm ${this.name}`;
}
}]);Sets up prototype chain inheritance between constructor functions.
/**
* Sets up prototype chain inheritance
* @param subClass - Child constructor function
* @param superClass - Parent constructor function
*/
function inherits(subClass: Function, superClass: Function): void;Loose mode inheritance with smaller bundle size but less spec compliance.
/**
* Loose mode inheritance (smaller bundle)
* @param subClass - Child constructor function
* @param superClass - Parent constructor function
*/
function inheritsLoose(subClass: Function, superClass: Function): void;Usage Example:
import _inherits from '@babel/runtime/helpers/esm/inherits';
import _createSuper from '@babel/runtime/helpers/esm/createSuper';
function Child() {
return _createSuper(Child).apply(this, arguments);
}
_inherits(Child, Parent);Creates a super constructor function for inheritance.
/**
* Creates super constructor function
* @param Derived - Derived class constructor
* @returns Function that calls parent constructor
*/
function createSuper(Derived: Function): (...args: any[]) => any;Handles super() calls in constructors with proper this binding.
/**
* Handles super() calls in constructors
* @param t - Current instance
* @param o - Derived constructor
* @param e - Arguments array
* @returns Result of super constructor call
*/
function callSuper(t: any, o: Function, e: any[]): any;Validates constructor return values and ensures proper this binding.
/**
* Validates constructor return values
* @param self - Current instance
* @param call - Result of super constructor call
* @returns Validated instance
*/
function possibleConstructorReturn(self: any, call: any): any;Ensures this is properly initialized in constructors.
/**
* Ensures 'this' is initialized in constructors
* @param self - Current instance
* @returns The validated instance
* @throws ReferenceError if this is uninitialized
*/
function assertThisInitialized(self: any): any;Gets object prototype with fallbacks for older environments.
/**
* Gets object prototype with fallbacks
* @param o - Object to get prototype from
* @returns Prototype object
*/
function getPrototypeOf(o: any): any;Sets object prototype with fallbacks for older environments.
/**
* Sets object prototype with fallbacks
* @param o - Object to set prototype on
* @param p - Prototype to set
* @returns The object with new prototype
*/
function setPrototypeOf(o: any, p: any): any;Gets property from prototype chain with super property semantics.
/**
* Gets property from prototype chain
* @param target - Target object
* @param property - Property name
* @param receiver - Receiver for getter calls
* @returns Property value
*/
function get(target: any, property: PropertyKey, receiver?: any): any;Sets property in prototype chain with super property semantics.
/**
* Sets property in prototype chain
* @param target - Target object
* @param property - Property name
* @param value - Value to set
* @param receiver - Receiver for setter calls
* @returns Success boolean
*/
function set(target: any, property: PropertyKey, value: any, receiver?: any): boolean;Base helper for super property access operations.
/**
* Base for super property access
* @param object - Current object
* @param property - Property being accessed
* @returns Target object for property access
*/
function superPropBase(object: any, property: PropertyKey): any;Gets super property with proper receiver binding.
/**
* Gets super property
* @param object - Current object
* @param property - Property to get
* @param receiver - Receiver for getter
* @param isStrict - Strict mode flag
* @returns Property value
*/
function superPropGet(object: any, property: PropertyKey, receiver: any, isStrict?: boolean): any;Sets super property with proper receiver binding.
/**
* Sets super property
* @param object - Current object
* @param property - Property to set
* @param value - Value to set
* @param receiver - Receiver for setter
* @param isStrict - Strict mode flag
* @returns Success boolean
*/
function superPropSet(object: any, property: PropertyKey, value: any, receiver: any, isStrict?: boolean): boolean;Calls constructor with arguments, handling native constructors properly.
/**
* Calls constructor with arguments
* @param Parent - Constructor function
* @param args - Constructor arguments
* @param Class - Actual class being constructed
* @returns New instance
*/
function construct(Parent: Function, args: any[], Class?: Function): any;Wraps native constructors (Array, Error, etc.) for proper extension.
/**
* Wraps native constructors for extension
* @param Class - Native constructor to wrap
* @returns Wrapped constructor function
*/
function wrapNativeSuper(Class: Function): Function;Checks if a function is a native browser/engine function.
/**
* Checks if function is native
* @param fn - Function to test
* @returns True if native function
*/
function isNativeFunction(fn: Function): boolean;Checks if native Reflect.construct is available and working.
/**
* Checks for native Reflect.construct
* @returns True if native Reflect.construct available
*/
function isNativeReflectConstruct(): boolean;// Constructor function interface
interface ClassConstructor extends Function {
prototype: any;
}
// Property descriptor for createClass
interface PropertyDescriptor {
key: PropertyKey;
value?: any;
get?: () => any;
set?: (value: any) => void;
kind: 'method' | 'get' | 'set';
static?: boolean;
}
// Property key types
type PropertyKey = string | number | symbol;
// Super constructor function type
type SuperConstructor = (...args: any[]) => any;