A TypeScript library that provides tolerable Mixin functionality with multiple inheritance support.
npx @tessl/cli install tessl/npm-ts-mixer@6.0.00
# ts-mixer
1
2
ts-mixer is a TypeScript library that provides comprehensive mixin functionality, enabling multiple inheritance in TypeScript and JavaScript projects. It supports mixing plain classes, classes that extend other classes, abstract classes, generic classes, and classes with decorators, offering multiple mixing strategies and sophisticated constructor parameter handling.
3
4
## Package Information
5
6
- **Package Name**: ts-mixer
7
- **Package Type**: npm
8
- **Language**: TypeScript
9
- **Installation**: `npm install ts-mixer`
10
11
## Core Imports
12
13
```typescript
14
import { Mixin, mix, hasMixin, decorate, settings } from "ts-mixer";
15
```
16
17
For CommonJS:
18
19
```javascript
20
const { Mixin, mix, hasMixin, decorate, settings } = require("ts-mixer");
21
```
22
23
## Basic Usage
24
25
```typescript
26
import { Mixin } from "ts-mixer";
27
28
class Foo {
29
protected makeFoo() {
30
return "foo";
31
}
32
}
33
34
class Bar {
35
protected makeBar() {
36
return "bar";
37
}
38
}
39
40
class FooBar extends Mixin(Foo, Bar) {
41
public makeFooBar() {
42
return this.makeFoo() + this.makeBar();
43
}
44
}
45
46
const fooBar = new FooBar();
47
console.log(fooBar.makeFooBar()); // "foobar"
48
```
49
50
## Architecture
51
52
ts-mixer is built around several key components:
53
54
- **Mixin Function**: Core function that creates mixed classes using prototype copying or ES6 proxies
55
- **Decorator System**: Support for preserving and inheriting decorators through mixing
56
- **Type Safety**: Full TypeScript support with complex generic type inference for up to 10 classes
57
- **Multiple Strategies**: Configurable strategies for prototype and static property handling
58
- **Mixin Tracking**: Internal system for tracking mixin relationships and providing instanceof replacement
59
- **Constructor Handling**: Sophisticated handling of constructor parameters and init functions
60
61
## Capabilities
62
63
### Core Mixing
64
65
Primary mixin functionality for creating classes that inherit from multiple base classes.
66
67
```typescript { .api }
68
function Mixin<A extends any[], I1, S1>(
69
c1: Class<A, I1, S1>
70
): Class<A, I1, S1>;
71
72
function Mixin<
73
A1 extends any[], I1, S1,
74
A2 extends any[], I2, S2
75
>(
76
c1: Class<A1, I1, S1>,
77
c2: Class<A2, I2, S2>
78
): Class<Longest<A1, A2>, I1 & I2, S1 & S2>;
79
80
// ... overloads for up to 10 classes
81
```
82
83
[Core Mixing](./core-mixing.md)
84
85
### Generic Class Support
86
87
Specialized mixing support for generic classes using the decorator pattern.
88
89
```typescript { .api }
90
function mix(...ingredients: Class[]): (decoratedClass: any) => any;
91
```
92
93
[Generic Classes](./generic-classes.md)
94
95
### Mixin Detection
96
97
Type-safe mixin detection and type narrowing functionality.
98
99
```typescript { .api }
100
function hasMixin<M>(
101
instance: any,
102
mixin: abstract new (...args) => M
103
): instance is M;
104
```
105
106
[Mixin Detection](./mixin-detection.md)
107
108
### Decorator Support
109
110
Decorator preservation and inheritance system for mixed classes.
111
112
```typescript { .api }
113
function decorate<T extends ClassDecorator | PropertyDecorator | MethodDecorator>(
114
decorator: T
115
): T;
116
```
117
118
[Decorators](./decorators.md)
119
120
### Configuration
121
122
Global configuration settings for controlling mixin behavior.
123
124
```typescript { .api }
125
interface Settings {
126
initFunction: string | null;
127
staticsStrategy: "copy" | "proxy";
128
prototypeStrategy: "copy" | "proxy";
129
decoratorInheritance: "deep" | "direct" | "none";
130
}
131
132
declare const settings: Settings;
133
```
134
135
[Configuration](./configuration.md)
136
137
## Types
138
139
```typescript { .api }
140
type Class<
141
CtorArgs extends any[] = any[],
142
InstanceType = {},
143
StaticType = {},
144
IsAbstract = false
145
> = (abstract new(...args: any[]) => InstanceType) & StaticType;
146
147
type Longest<
148
T1 extends any[],
149
T2 extends any[] = [],
150
T3 extends any[] = [],
151
T4 extends any[] = [],
152
T5 extends any[] = [],
153
T6 extends any[] = [],
154
T7 extends any[] = [],
155
T8 extends any[] = [],
156
T9 extends any[] = [],
157
T10 extends any[] = []
158
> = /* complex conditional type for longest tuple */;
159
```