rtt 1.1.2 → 1.2.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 +20 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.js +10 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,10 +37,14 @@ export type Animal = {$type: $Type; name: string};
|
|
|
37
37
|
export type Dog = Animal & { breed: string };
|
|
38
38
|
export type Cat = Animal & { lives: number };
|
|
39
39
|
|
|
40
|
-
// 2️⃣ Create typed objects
|
|
40
|
+
// 2️⃣ Create typed objects — manual
|
|
41
41
|
const rover: Dog = {$type: $Dog, name: 'Rover', breed: 'Labrador'};
|
|
42
42
|
const whiskers: Cat = {$type: $Cat, name: 'Whiskers', lives: 9};
|
|
43
43
|
|
|
44
|
+
// 2️⃣ Create typed objects — with factory (shorthand)
|
|
45
|
+
const rover = $Dog({ name: 'Rover', breed: 'Labrador' });
|
|
46
|
+
const whiskers = $Cat({ name: 'Whiskers', lives: 9 });
|
|
47
|
+
|
|
44
48
|
// 3️⃣ Check types at runtime
|
|
45
49
|
is(rover, $Dog); // ✅ true
|
|
46
50
|
is(rover, $Animal); // ✅ true — inheritance works!
|
|
@@ -129,6 +133,21 @@ const $Dog = $type<Dog>('app.Dog', $Animal);
|
|
|
129
133
|
| `parent?` | `$Type \| null` | Parent type for inheritance chains |
|
|
130
134
|
| `generics?` | `$Type[]` | Generic type arguments to compare |
|
|
131
135
|
|
|
136
|
+
### `$type(value)` → `T`
|
|
137
|
+
|
|
138
|
+
Factory method on every `$Type` descriptor. Creates a new instance with the `$type` tag automatically attached — no need to manually spread `{ $type: type, ... }`:
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
const rover = $Dog({ name: 'Rover', breed: 'Labrador' });
|
|
142
|
+
// ✅ { $type: $Dog, name: 'Rover', breed: 'Labrador' }
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
| Param | Type | Description |
|
|
146
|
+
|---|---|---|
|
|
147
|
+
| `value` | `Omit<T, '$type'>` | Object without the `$type` property |
|
|
148
|
+
|
|
149
|
+
**Returns**: A new object of type `T` with `$type` automatically set to the descriptor.
|
|
150
|
+
|
|
132
151
|
### `is<T>(value, type)` → `value is T`
|
|
133
152
|
|
|
134
153
|
Runtime type guard. Returns `true` if `value.$type` matches `type` or any of its ancestors in the hierarchy.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
export declare interface $Type<T =
|
|
2
|
-
|
|
1
|
+
export declare interface $Type<T = any> {
|
|
2
|
+
(value: Omit<T, '$type'>): T;
|
|
3
|
+
typeName: string;
|
|
3
4
|
type?: T;
|
|
4
5
|
parent?: $Type | null | undefined;
|
|
5
6
|
generics?: $Type[] | null | undefined;
|
|
6
7
|
}
|
|
7
8
|
|
|
8
|
-
export declare function $type<T
|
|
9
|
+
export declare function $type<T>(name: string, parent?: $Type | null | undefined, generics?: $Type[] | null | undefined): $Type<T>;
|
|
9
10
|
|
|
10
11
|
export declare type Brand<T extends string> = {
|
|
11
12
|
[brand]?: T;
|
package/dist/index.js
CHANGED
|
@@ -8,16 +8,21 @@ function isType(a, b) {
|
|
|
8
8
|
if (a.generics && a.generics.length === b.generics.length) return a.generics.every((x, i) => isType(x, b.generics[i]));
|
|
9
9
|
return false;
|
|
10
10
|
}
|
|
11
|
-
return a.
|
|
11
|
+
return a.typeName === b.typeName;
|
|
12
12
|
}
|
|
13
13
|
//#endregion
|
|
14
14
|
//#region src/type/type.ts
|
|
15
15
|
function $type(name, parent, generics) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
const type = function(value) {
|
|
17
|
+
return {
|
|
18
|
+
$type: type,
|
|
19
|
+
...value
|
|
20
|
+
};
|
|
20
21
|
};
|
|
22
|
+
type.typeName = name;
|
|
23
|
+
type.parent = parent;
|
|
24
|
+
type.generics = generics;
|
|
25
|
+
return type;
|
|
21
26
|
}
|
|
22
27
|
//#endregion
|
|
23
28
|
export { $type, is, isType };
|