composition-ts 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,38 +1,72 @@
1
1
  # Composition TS
2
2
 
3
- Compose any number of types into a single intersection type.
3
+ Instead of inheritance, compose prototype parts into a generated class.
4
4
 
5
5
  ## Usage
6
6
 
7
7
  ```ts
8
- import type { Compose } from 'composition-ts';
8
+ import { compose } from 'composition-ts';
9
9
 
10
- type Result = Compose<[{ id: number }, { name: string }, { active: boolean }]>;
10
+ const statePart = {
11
+ id: 0,
12
+ name: '',
13
+ };
11
14
 
12
- // Result:
13
- // { id: number } & { name: string } & { active: boolean }
15
+ const renamable = {
16
+ rename(this: typeof statePart, next: string) {
17
+ this.name = next;
18
+ return this.name;
19
+ },
20
+ };
21
+
22
+ const describable = {
23
+ get label() {
24
+ const self = this as typeof statePart;
25
+ return `${self.id}:${self.name}`;
26
+ },
27
+ };
28
+
29
+ const PersonModel = compose(
30
+ function Person(this: Person, id: number, name: string) {
31
+ this.id = id;
32
+ this.name = name;
33
+ },
34
+ [statePart, renamable, describable],
35
+ 'PersonModel',
36
+ );
37
+
38
+ const person = new PersonModel(1, 'Ada');
39
+
40
+ person.rename('Grace');
41
+ console.log(person.label); // 1:Grace
14
42
  ```
15
43
 
16
44
  ## API
17
45
 
18
- ### Compose<Types>
46
+ ### compose(initializer, parts, className?)
19
47
 
20
- Intersects every type in the tuple from left to right.
48
+ Creates a new class and copies every property descriptor from `parts` onto its prototype.
21
49
 
22
50
  ```ts
23
- type Empty = Compose<[]>; // unknown
24
- type Single = Compose<[string]>; // string
25
- type Mixed = Compose<[string, number]>; // never
51
+ declare function compose<Protos extends readonly object[], Args extends any[], Instance extends Compose<Protos>>(
52
+ initializer: (this: Instance, ...args: Args) => any,
53
+ parts: Protos,
54
+ className?: string,
55
+ ): new (...args: Args) => Instance;
26
56
  ```
27
57
 
28
- ### IntersectAll<Types>
58
+ Parameters:
29
59
 
30
- Alias of Compose<Types>.
60
+ - `initializer`: A regular function invoked inside the generated constructor with the new instance bound as `this`.
61
+ - `parts`: An array of objects whose own property descriptors are copied onto the generated class prototype.
62
+ - `className`: Optional class name for the generated constructor. Defaults to `initializer.name`.
31
63
 
32
- ## License
64
+ Notes:
33
65
 
34
- MIT
66
+ - `initializer` must be a regular function. Arrow functions cannot receive the instance through `this`.
67
+ - Property descriptors are copied as-is, so getters, setters, enumerability, and writability are preserved.
68
+ - When multiple parts define the same key, later parts overwrite earlier ones.
35
69
 
36
- ```
70
+ ## License
37
71
 
38
- ```
72
+ MIT
package/dist/index.d.ts CHANGED
@@ -13,13 +13,13 @@ type Compose<Types extends readonly unknown[]> = Types extends readonly [infer H
13
13
  * ## About
14
14
  * @package CompositionTs
15
15
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
16
- * @version 0.0.0 (Last Update: 2026.03.26 10:24:23.468)
16
+ * @version 0.1.0 (Last Update: 2026.03.26 11:15:42.072)
17
17
  * @license MIT
18
18
  * @link git+https://github.com/baendlorel/kt-packages.git
19
19
  * @link https://baendlorel.github.io/ Welcome to my site!
20
20
  * @description Instead of inheritance, make it composition!
21
21
  * @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
22
22
  */
23
- declare function compose<Protos extends readonly object[], Args extends any[], Instance extends Compose<Protos>>(initializer: (this: Instance, ...args: Args) => any, parts: Protos, className?: string): new (...args: Args) => Instance;
23
+ declare function compose<Protos extends readonly object[], Args extends any[], Instance extends Compose<Protos> = Compose<Protos>>(initializer: (this: Instance, ...args: Args) => any, parts: Protos, className?: string): new (...args: Args) => Instance;
24
24
 
25
25
  export { compose };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "composition-ts",
3
- "version": "0.0.0",
3
+ "version": "0.1.0",
4
4
  "author": {
5
5
  "name": "Kasukabe Tsumugi",
6
6
  "email": "futami16237@gmail.com"