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 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.
@@ -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.name === b.name;
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
- return {
17
- name,
18
- parent,
19
- generics
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
- "author": "Nizami",
3
- "bugs": {
4
- "url": "https://github.com/nizami/rtt/issues"
5
- },
2
+ "name": "rtt",
3
+ "version": "1.2.0",
6
4
  "description": "Runtime Typescript Types",
7
- "devDependencies": {
8
- "@types/node": "^25.9.3",
9
- "barrelize": "^1.8.1",
10
- "typescript": "~6.0.3",
11
- "vite": "^8.0.16",
12
- "vite-plugin-dts": "^5.0.2",
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
- "homepage": "https://github.com/nizami/rtt#readme",
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
- "license": "MIT",
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
- "scripts": {
37
- "build": "barrelize && vite build",
38
- "test": "vitest run"
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"}
@@ -1,5 +0,0 @@
1
- declare const brand: unique symbol;
2
- export type Brand<T extends string> = {
3
- [brand]?: T;
4
- };
5
- export {};
@@ -1,3 +0,0 @@
1
- export * from './brand';
2
- export * from './is/is';
3
- export * from './type/type';
@@ -1,3 +0,0 @@
1
- import { $Type } from '../type/type';
2
- export declare function is<T extends $Type>(value: any, type: T): value is Exclude<T['type'], undefined>;
3
- export declare function isType(a: $Type, b: $Type): boolean;
@@ -1 +0,0 @@
1
- export {};
@@ -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 {};