rtt 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Nizami
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # rtt
2
+
3
+ Runtime Typescript Types
4
+
5
+ ## Overview
6
+
7
+ This package provides a way to store type information in objects and check types at runtime in TypeScript. It is useful for scenarios where you need runtime type introspection, such as serialization, validation, or building type-safe APIs.
8
+
9
+ ## Features
10
+
11
+ - Attach type metadata to objects.
12
+ - Check if an object is of a specific type at runtime using the `is` function.
13
+ - Support for type hierarchies and generics.
14
+
15
+ ## Installation
16
+
17
+ ```sh
18
+ npm install rtt
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ```typescript
24
+ import {$NewType, $type, is, Model} from 'rtt';
25
+
26
+ // Define types
27
+ const $A = () => $NewType('MyPackage', 'A');
28
+ const $B = () => $NewType('MyPackage', 'B', $A());
29
+
30
+ // Create a model instance
31
+ const model: Model = {
32
+ [$type]: $B(),
33
+ };
34
+
35
+ // Runtime type checks
36
+ is(model, $B()); // true
37
+ is(model, $A()); // true (because B extends A)
38
+ ```
39
+
40
+ ## API
41
+
42
+ ### `$NewType(packageName: string, typeName: string, parent?, generics?)`
43
+
44
+ Creates a new runtime type.
45
+
46
+ ### `is(model: any, type: $Type): boolean`
47
+
48
+ Checks if the given model is of the specified type (or its parent).
49
+
50
+ ### `Model`
51
+
52
+ Interface for objects with runtime type information.
53
+
54
+ ---
@@ -0,0 +1,5 @@
1
+ declare const brand: unique symbol;
2
+ export type Brand<T extends `${string}.${string}`> = {
3
+ [brand]?: T;
4
+ };
5
+ export {};
@@ -0,0 +1,5 @@
1
+ export * from './brand';
2
+ export * from './is';
3
+ export * from './model';
4
+ export * from './type.factory';
5
+ export * from './type';
package/dist/index.js ADDED
@@ -0,0 +1,38 @@
1
+ function is(model, type) {
2
+ return model?.[$type]?.is(type) ?? false;
3
+ }
4
+ const $type = Symbol("type");
5
+ const $Model = () => $NewType("RuntimeType", "Model");
6
+ function $NewType(packageName, typeName, parent, generics) {
7
+ return {
8
+ packageName,
9
+ typeName,
10
+ parent,
11
+ generics,
12
+ toString() {
13
+ return `${this.packageName}.${this.typeName}`;
14
+ },
15
+ nameEquals(other) {
16
+ return this.typeName === other.typeName && this.packageName == other.packageName;
17
+ },
18
+ is(other) {
19
+ if (this.parent?.is(other) || other.nameEquals($Model())) {
20
+ return true;
21
+ }
22
+ if (other.generics && other.generics?.length > 0) {
23
+ if (this.generics && this.generics.length === other.generics.length) {
24
+ return this.generics.every((x, i) => x.is(other.generics[i]));
25
+ }
26
+ return false;
27
+ }
28
+ return this.nameEquals(other);
29
+ }
30
+ };
31
+ }
32
+ export {
33
+ $Model,
34
+ $NewType,
35
+ $type,
36
+ is
37
+ };
38
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/is.ts","../src/model.ts","../src/type.factory.ts"],"sourcesContent":["import {$Type, $type} from '#lib';\n\nexport function is<T extends $Type>(model: any, type: T): model is Exclude<T['type'], undefined> {\n return (model?.[$type] as $Type)?.is(type) ?? false;\n}\n","import { $Type, $NewType } from \"#lib\";\n\nexport const $type = Symbol('type');\n\nexport interface Model {\n [$type]: $Type;\n}\n\nexport const $Model = () => $NewType<Model>('RuntimeType', 'Model');\n","import {$Model, $Type, Model} from '#lib';\n\nexport function $NewType<T extends Model>(\n packageName: string,\n typeName: string,\n parent?: $Type | null | undefined,\n generics?: $Type[] | null | undefined,\n): $Type<T> {\n return {\n packageName,\n typeName,\n parent,\n generics,\n\n toString(): string {\n return `${this.packageName}.${this.typeName}`;\n },\n\n nameEquals(other: $Type): boolean {\n return this.typeName === other.typeName && this.packageName == other.packageName;\n },\n\n is(other: $Type): boolean {\n if (this.parent?.is(other) || other.nameEquals($Model())) {\n return true;\n }\n\n if (other.generics && other.generics?.length > 0) {\n if (this.generics && this.generics.length === other.generics.length) {\n // todo Covariance and contravariance ???\n return this.generics.every((x, i) => x.is(other.generics![i]));\n }\n\n return false;\n }\n\n return this.nameEquals(other);\n },\n };\n}\n"],"names":[],"mappings":"AAEO,SAAS,GAAoB,OAAY,MAAiD;AAC/F,SAAQ,QAAQ,KAAK,GAAa,GAAG,IAAI,KAAK;AAChD;ACFO,MAAM,QAAQ,OAAO,MAAM;AAM3B,MAAM,SAAS,MAAM,SAAgB,eAAe,OAAO;ACN3D,SAAS,SACd,aACA,UACA,QACA,UACU;AACV,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IAEA,WAAmB;AACjB,aAAO,GAAG,KAAK,WAAW,IAAI,KAAK,QAAQ;AAAA,IAC7C;AAAA,IAEA,WAAW,OAAuB;AAChC,aAAO,KAAK,aAAa,MAAM,YAAY,KAAK,eAAe,MAAM;AAAA,IACvE;AAAA,IAEA,GAAG,OAAuB;AACxB,UAAI,KAAK,QAAQ,GAAG,KAAK,KAAK,MAAM,WAAW,OAAA,CAAQ,GAAG;AACxD,eAAO;AAAA,MACT;AAEA,UAAI,MAAM,YAAY,MAAM,UAAU,SAAS,GAAG;AAChD,YAAI,KAAK,YAAY,KAAK,SAAS,WAAW,MAAM,SAAS,QAAQ;AAEnE,iBAAO,KAAK,SAAS,MAAM,CAAC,GAAG,MAAM,EAAE,GAAG,MAAM,SAAU,CAAC,CAAC,CAAC;AAAA,QAC/D;AAEA,eAAO;AAAA,MACT;AAEA,aAAO,KAAK,WAAW,KAAK;AAAA,IAC9B;AAAA,EAAA;AAEJ;"}
package/dist/is.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { $Type } from './index.ts';
2
+ export declare function is<T extends $Type>(model: any, type: T): model is Exclude<T['type'], undefined>;
@@ -0,0 +1,6 @@
1
+ import { $Type } from './index.ts';
2
+ export declare const $type: unique symbol;
3
+ export interface Model {
4
+ [$type]: $Type;
5
+ }
6
+ export declare const $Model: () => $Type<Model>;
package/dist/type.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ export interface $Type<T = unknown> {
2
+ packageName: string;
3
+ typeName: string;
4
+ type?: T;
5
+ parent?: $Type | null | undefined;
6
+ generics?: $Type[] | null | undefined;
7
+ toString(): string;
8
+ nameEquals(other: $Type): boolean;
9
+ is(other: $Type): boolean;
10
+ }
@@ -0,0 +1,2 @@
1
+ import { $Type, Model } from './index.ts';
2
+ export declare function $NewType<T extends Model>(packageName: string, typeName: string, parent?: $Type | null | undefined, generics?: $Type[] | null | undefined): $Type<T>;
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "rtt",
3
+ "version": "1.0.0",
4
+ "description": "Runtime Typescript Types",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "build": "barrelize && vite build",
8
+ "test": "vitest run"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/nizami/rtt.git"
13
+ },
14
+ "keywords": [
15
+ "Typescript",
16
+ "Runtime Types"
17
+ ],
18
+ "author": "Nizami",
19
+ "license": "MIT",
20
+ "bugs": {
21
+ "url": "https://github.com/nizami/rtt/issues"
22
+ },
23
+ "homepage": "https://github.com/nizami/rtt#readme",
24
+ "devDependencies": {
25
+ "@types/node": "^24.0.10",
26
+ "barrelize": "^1.6.2",
27
+ "typescript": "~5.8.3",
28
+ "vite": "^7.0.2",
29
+ "vite-plugin-dts": "^4.5.4",
30
+ "vitest": "^3.2.4"
31
+ },
32
+ "type": "module",
33
+ "sideEffects": false,
34
+ "files": [
35
+ "dist"
36
+ ],
37
+ "types": "./dist/index.d.ts",
38
+ "exports": {
39
+ ".": {
40
+ "types": "./dist/index.d.ts",
41
+ "import": "./dist/index.js"
42
+ }
43
+ }
44
+ }