rtt 1.1.1 → 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 +21 -0
- package/dist/index.js +10 -7
- package/package.json +24 -23
- package/dist/index.js.map +0 -1
- package/dist/src/brand.d.ts +0 -5
- package/dist/src/index.d.ts +0 -3
- package/dist/src/is/is.d.ts +0 -3
- package/dist/src/is/is.test.d.ts +0 -1
- package/dist/src/type/type.d.ts +0 -7
- package/dist/src/type/type.test.d.ts +0 -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
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare interface $Type<T = any> {
|
|
2
|
+
(value: Omit<T, '$type'>): T;
|
|
3
|
+
typeName: string;
|
|
4
|
+
type?: T;
|
|
5
|
+
parent?: $Type | null | undefined;
|
|
6
|
+
generics?: $Type[] | null | undefined;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export declare function $type<T>(name: string, parent?: $Type | null | undefined, generics?: $Type[] | null | undefined): $Type<T>;
|
|
10
|
+
|
|
11
|
+
export declare type Brand<T extends string> = {
|
|
12
|
+
[brand]?: T;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
declare const brand: unique symbol;
|
|
16
|
+
|
|
17
|
+
export declare function is<T extends $Type>(value: any, type: T): value is Exclude<T['type'], undefined>;
|
|
18
|
+
|
|
19
|
+
export declare function isType(a: $Type, b: $Type): boolean;
|
|
20
|
+
|
|
21
|
+
export { }
|
package/dist/index.js
CHANGED
|
@@ -8,18 +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 };
|
|
24
|
-
|
|
25
|
-
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,17 +1,13 @@
|
|
|
1
1
|
{
|
|
2
|
-
"
|
|
3
|
-
"
|
|
4
|
-
"url": "https://github.com/nizami/rtt/issues"
|
|
5
|
-
},
|
|
2
|
+
"name": "rtt",
|
|
3
|
+
"version": "1.2.0",
|
|
6
4
|
"description": "Runtime Typescript Types",
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"vitest": "^4.1.8"
|
|
14
|
-
},
|
|
5
|
+
"author": "Nizami",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"main": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
15
11
|
"exports": {
|
|
16
12
|
".": {
|
|
17
13
|
"import": "./dist/index.js",
|
|
@@ -21,26 +17,31 @@
|
|
|
21
17
|
"files": [
|
|
22
18
|
"dist"
|
|
23
19
|
],
|
|
24
|
-
"
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "barrelize && vite build",
|
|
22
|
+
"test": "barrelize && vitest run"
|
|
23
|
+
},
|
|
25
24
|
"keywords": [
|
|
26
25
|
"Typescript",
|
|
27
26
|
"Runtime Types"
|
|
28
27
|
],
|
|
29
|
-
"
|
|
30
|
-
"main": "index.js",
|
|
31
|
-
"name": "rtt",
|
|
28
|
+
"homepage": "https://github.com/nizami/rtt#readme",
|
|
32
29
|
"repository": {
|
|
33
30
|
"type": "git",
|
|
34
31
|
"url": "git+https://github.com/nizami/rtt.git"
|
|
35
32
|
},
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/nizami/rtt/issues"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@microsoft/api-extractor": "^7.58.9",
|
|
38
|
+
"@types/node": "^25.9.3",
|
|
39
|
+
"barrelize": "^1.8.1",
|
|
40
|
+
"typescript": "~6.0.3",
|
|
41
|
+
"unplugin-dts": "^1.0.2",
|
|
42
|
+
"vite": "^8.0.16",
|
|
43
|
+
"vitest": "^4.1.8"
|
|
39
44
|
},
|
|
40
|
-
"sideEffects": false,
|
|
41
|
-
"type": "module",
|
|
42
|
-
"types": "./dist/index.d.ts",
|
|
43
|
-
"version": "1.1.1",
|
|
44
45
|
"allowScripts": {
|
|
45
46
|
"fsevents@2.3.3": true
|
|
46
47
|
}
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/is/is.ts","../src/type/type.ts"],"sourcesContent":["import {$Type} from '../type/type';\n\nexport function is<T extends $Type>(value: any, type: T): value is Exclude<T['type'], undefined> {\n return '$type' in value && isType(value?.$type, type);\n}\n\nexport function isType(a: $Type, b: $Type): boolean {\n if (a.parent && isType(a.parent, b)) {\n return true;\n }\n\n if (b.generics && b.generics.length > 0) {\n if (a.generics && a.generics.length === b.generics.length) {\n // todo Covariance and Contravariance ???\n return a.generics.every((x, i) => isType(x, b.generics![i]));\n }\n\n return false;\n }\n\n return a.name === b.name;\n}\n","export interface $Type<T = unknown> {\n name: string;\n type?: T;\n parent?: $Type | null | undefined;\n generics?: $Type[] | null | undefined;\n}\n\nexport function $type<T = unknown>(\n name: string,\n parent?: $Type | null | undefined,\n generics?: $Type[] | null | undefined,\n): $Type<T> {\n return {name, parent, generics};\n}\n"],"mappings":";AAEA,SAAgB,GAAoB,OAAY,MAAiD;CAC/F,OAAO,WAAW,SAAS,OAAO,OAAO,OAAO,IAAI;AACtD;AAEA,SAAgB,OAAO,GAAU,GAAmB;CAClD,IAAI,EAAE,UAAU,OAAO,EAAE,QAAQ,CAAC,GAChC,OAAO;CAGT,IAAI,EAAE,YAAY,EAAE,SAAS,SAAS,GAAG;EACvC,IAAI,EAAE,YAAY,EAAE,SAAS,WAAW,EAAE,SAAS,QAEjD,OAAO,EAAE,SAAS,OAAO,GAAG,MAAM,OAAO,GAAG,EAAE,SAAU,EAAE,CAAC;EAG7D,OAAO;CACT;CAEA,OAAO,EAAE,SAAS,EAAE;AACtB;;;ACdA,SAAgB,MACd,MACA,QACA,UACU;CACV,OAAO;EAAC;EAAM;EAAQ;CAAQ;AAChC"}
|
package/dist/src/brand.d.ts
DELETED
package/dist/src/index.d.ts
DELETED
package/dist/src/is/is.d.ts
DELETED
package/dist/src/is/is.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/src/type/type.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export interface $Type<T = unknown> {
|
|
2
|
-
name: string;
|
|
3
|
-
type?: T;
|
|
4
|
-
parent?: $Type | null | undefined;
|
|
5
|
-
generics?: $Type[] | null | undefined;
|
|
6
|
-
}
|
|
7
|
-
export declare function $type<T = unknown>(name: string, parent?: $Type | null | undefined, generics?: $Type[] | null | undefined): $Type<T>;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|