@liautaud/typezod 1.0.0 → 1.0.1

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.
@@ -172,5 +172,4 @@ declare const t: {
172
172
  */
173
173
  declare function isAssignableTo(ctx: SchemaContext, type: Type, schema: TypeSchema): boolean;
174
174
  //#endregion
175
- export { SchemaContext, TypeSchema, isAssignableTo, t };
176
- //# sourceMappingURL=index.d.ts.map
175
+ export { SchemaContext, TypeSchema, isAssignableTo, t };
@@ -55,7 +55,7 @@ var TypeSchema = class TypeSchema {
55
55
  }
56
56
  };
57
57
  const schema = (check) => new TypeSchema(check);
58
- const moduleTypeCache = new WeakMap();
58
+ const moduleTypeCache = /* @__PURE__ */ new WeakMap();
59
59
  /**
60
60
  * Schema builder namespace.
61
61
  */
@@ -89,7 +89,7 @@ const t = {
89
89
  if (!program) throw new Error("t.fromModule() requires `program` in the SchemaContext.");
90
90
  let cache = moduleTypeCache.get(checker);
91
91
  if (!cache) {
92
- cache = new Map();
92
+ cache = /* @__PURE__ */ new Map();
93
93
  moduleTypeCache.set(checker, cache);
94
94
  }
95
95
  const cacheKey = JSON.stringify([modulePathPattern, exportName]);
@@ -123,5 +123,4 @@ function isAssignableTo(ctx, type, schema$1) {
123
123
  }
124
124
 
125
125
  //#endregion
126
- export { TypeSchema, isAssignableTo, t };
127
- //# sourceMappingURL=index.js.map
126
+ export { TypeSchema, isAssignableTo, t };
package/package.json CHANGED
@@ -1,14 +1,18 @@
1
1
  {
2
2
  "name": "@liautaud/typezod",
3
- "version": "1.0.0",
4
- "description": "Zod-like DSL for comparing TypeScript compiler types against user-defined schemas",
5
3
  "type": "module",
6
- "license": "MIT",
4
+ "version": "1.0.1",
5
+ "description": "Zod-like DSL for comparing TypeScript compiler types against user-defined schemas",
7
6
  "author": "Romain Liautaud",
7
+ "license": "MIT",
8
+ "homepage": "https://github.com/liautaud/typezod#readme",
8
9
  "repository": {
9
10
  "type": "git",
10
11
  "url": "git+https://github.com/liautaud/typezod.git"
11
12
  },
13
+ "bugs": {
14
+ "url": "https://github.com/liautaud/typezod/issues"
15
+ },
12
16
  "keywords": [
13
17
  "typescript",
14
18
  "type-checking",
@@ -32,16 +36,19 @@
32
36
  },
33
37
  "scripts": {
34
38
  "build": "tsdown",
39
+ "dev": "tsdown --watch",
40
+ "test": "vitest",
35
41
  "typecheck": "tsc --noEmit",
36
- "test": "vitest run",
37
42
  "prepublishOnly": "npm test && npm run build"
38
43
  },
39
44
  "peerDependencies": {
40
45
  "typescript": ">=5.0.0"
41
46
  },
42
47
  "devDependencies": {
43
- "tsdown": "^0.11.9",
48
+ "@types/node": "^25.0.3",
49
+ "prettier": "^3.8.1",
50
+ "tsdown": "^0.18.1",
44
51
  "typescript": "^5.9.3",
45
- "vitest": "^3.2.4"
52
+ "vitest": "^4.0.16"
46
53
  }
47
54
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"sourcesContent":[],"mappings":";;;;;;AAQA;;AACW,KADC,aAAA,GACD;EAAW,OACV,EADD,WACC;EAAO,OAAA,CAAA,EAAP,OAAO;AACjB,CAAA;KAEG,SAAA,GAAS,CAAA,IAAA,EAAU,IAAV,EAAA,GAAA,EAAqB,aAArB,EAAA,GAAA,OAAA;;;AAAkC;AAQhD;;;AAQuB,cARV,UAAA,CAQU;EAAS;EAiBR,SAYV,QAAA,EAnCO,SAmCP;EAAU;EAYD,SAAA,WAAA,EAAA,OAAA;EAaV;EA+MZ,WAAA,CAAA,OAAA,EArQsB,SAqQtB,EAAA,UAAA,CAAA,EAAA,OAAA;EAAA;;;;;;;;;;;;EAjH4B,QAAG,CAAA,CAAA,EAnIlB,UAmIkB;EAAU;;;;;;;AAgHE;EAQ5B,QAAA,CAAA,CAAA,EA/OF,UA+OgB;EAAA;;;;AAGV;;;;aAtOP;;;;;cAaA;;gBAEC;;gBAMA;;iBAMC;;cAMH;;mBAMK;;cAML;;;;;aASD;;;;;iBAMI;;;;;;;;;;;;;;;;;kBAkBG,eAAe,gBAAc;;;;;;;;;;;mBA6B5B,eAAa;;;;;;;;;;sBAgBV,iBAAe;;;;;;;;;;;;;6BAsBR,iBAAe;;;;;;;;;;;;;;;;;;;;iEAsBmB;;;;;;;;;;;sBAoDzC,cAAY;;;;;;;iBAQlB,cAAA,MACT,qBACC,cACE"}
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","names":["accepts: AcceptsFn","check: AcceptsFn","shape: Record<string, TypeSchema>","element: TypeSchema","modulePathPattern: string","exportName: string","resolved: Type | null","predicate: AcceptsFn","ctx: SchemaContext","type: Type","schema: TypeSchema"],"sources":["../src/index.ts"],"sourcesContent":["import type { TypeChecker, Type, Program, SymbolFlags } from \"typescript\";\n\nconst TS_SYMBOL_FLAGS_OPTIONAL = 16777216 satisfies SymbolFlags.Optional;\n\n/**\n * Context required by all schema checks. The `program` field is only needed\n * for schemas that resolve types from external modules ({@link t.fromModule}).\n */\nexport type SchemaContext = {\n checker: TypeChecker;\n program?: Program;\n};\n\ntype AcceptsFn = (type: Type, ctx: SchemaContext) => boolean;\n\n/**\n * A type schema that checks whether a TypeScript compiler type is assignable\n * to the type represented by the schema.\n *\n * Obtain instances through the {@link t} builder, never instantiate directly.\n */\nexport class TypeSchema {\n /** @internal */\n readonly _accepts: AcceptsFn;\n\n /** @internal */\n readonly _isOptional: boolean;\n\n /** @internal */\n constructor(accepts: AcceptsFn, isOptional = false) {\n this._accepts = accepts;\n this._isOptional = isOptional;\n }\n\n /**\n * Marks this schema as optional when used as a property in {@link t.object}.\n * An optional property may be absent from the type or carry the TypeScript\n * `Optional` symbol flag.\n *\n * @example\n * ```typescript\n * const Shape = t.object({\n * label: t.string().optional(),\n * });\n * ```\n */\n optional(): TypeSchema {\n return new TypeSchema(t.union(this, t.undefined())._accepts, true);\n }\n\n /**\n * Wraps this schema to also accept `null`.\n *\n * @example\n * ```typescript\n * const MaybeNumber = t.number().nullable(); // number | null\n * ```\n */\n nullable(): TypeSchema {\n return t.union(this, t.null());\n }\n\n /**\n * Wraps this schema to also accept `null` and `undefined`.\n *\n * @example\n * ```typescript\n * const MaybeNumber = t.number().nullish(); // number | null | undefined\n * ```\n */\n nullish(): TypeSchema {\n return this.nullable().optional();\n }\n}\n\nconst schema = (check: AcceptsFn): TypeSchema => new TypeSchema(check);\n\n// Per-checker cache for `t.fromModule()` resolved types.\nconst moduleTypeCache = new WeakMap<TypeChecker, Map<string, Type | null>>();\n\n/**\n * Schema builder namespace.\n */\nexport const t = {\n /** Represents the `number` type. */\n number: (): TypeSchema =>\n schema((type, { checker }) =>\n checker.isTypeAssignableTo(type, checker.getNumberType())\n ),\n\n /** Represents the `string` type. */\n string: (): TypeSchema =>\n schema((type, { checker }) =>\n checker.isTypeAssignableTo(type, checker.getStringType())\n ),\n\n /** Represents the `boolean` type. */\n boolean: (): TypeSchema =>\n schema((type, { checker }) =>\n checker.isTypeAssignableTo(type, checker.getBooleanType())\n ),\n\n /** Represents the `void` type. */\n void: (): TypeSchema =>\n schema((type, { checker }) =>\n checker.isTypeAssignableTo(type, checker.getVoidType())\n ),\n\n /** Represents the `undefined` type. */\n undefined: (): TypeSchema =>\n schema((type, { checker }) =>\n checker.isTypeAssignableTo(type, checker.getUndefinedType())\n ),\n\n /** Represents the `null` type. */\n null: (): TypeSchema =>\n schema((type, { checker }) =>\n checker.isTypeAssignableTo(type, checker.getNullType())\n ),\n\n /**\n * Represents the `any` type. Useful as a placeholder in object schemas when\n * you care about a property's existence but not its type.\n */\n any: (): TypeSchema => schema(() => true),\n\n /**\n * Represents the `unknown` type. Semantically identical to `t.any()` as a\n * predicate, but communicates intent differently in your schema.\n */\n unknown: (): TypeSchema => schema(() => true),\n\n /**\n * Represents an object type with the specified shape. Each property must be\n * present and non-optional unless its schema was marked `.optional()`.\n *\n * Extra properties are allowed (structural subtyping), consistent with\n * TypeScript's own assignability rules.\n *\n * @example\n * ```typescript\n * const Point = t.object({\n * x: t.number(),\n * y: t.number(),\n * label: t.string().optional(),\n * });\n * ```\n */\n object: (shape: Record<string, TypeSchema>): TypeSchema =>\n schema((type, ctx) =>\n Object.entries(shape).every(([key, propSchema]) => {\n const propSymbol = type.getProperty(key);\n\n if (!propSymbol) return propSchema._isOptional;\n\n if (\n !propSchema._isOptional &&\n (propSymbol.flags & TS_SYMBOL_FLAGS_OPTIONAL) !== 0\n ) {\n return false;\n }\n\n const propType = ctx.checker.getTypeOfSymbol(propSymbol);\n return propSchema._accepts(propType, ctx);\n })\n ),\n\n /**\n * Represents an array type whose element type satisfies the given schema.\n * Checks for a numeric index signature, so it also covers tuples and other\n * numerically-indexed types.\n *\n * @example\n * ```typescript\n * const NumberArray = t.array(t.number());\n * ```\n */\n array: (element: TypeSchema): TypeSchema =>\n schema((type, ctx) => {\n const indexType = type.getNumberIndexType();\n if (!indexType) return false;\n return element._accepts(indexType, ctx);\n }),\n\n /**\n * Represents a union of the given schemas. For union source types, every\n * constituent must individually satisfy at least one member schema.\n *\n * @example\n * ```typescript\n * const StringOrNumber = t.union(t.string(), t.number());\n * ```\n */\n union: (...members: TypeSchema[]): TypeSchema =>\n schema((type, ctx) => {\n if (type.isUnion()) {\n return type.types.every((member) =>\n members.some((m) => m._accepts(member, ctx))\n );\n }\n return members.some((m) => m._accepts(type, ctx));\n }),\n\n /**\n * Represents an intersection of the given schemas. The type must satisfy\n * every member schema simultaneously.\n *\n * @example\n * ```typescript\n * const NamedAndAged = t.intersection(\n * t.object({ name: t.string() }),\n * t.object({ age: t.number() }),\n * );\n * ```\n */\n intersection: (...members: TypeSchema[]): TypeSchema =>\n schema((type, ctx) => members.every((m) => m._accepts(type, ctx))),\n\n /**\n * Represents a type exported from a module in the program. Resolves a genuine\n * `ts.Type` and delegates to `checker.isTypeAssignableTo()`, so it handles\n * nominal class hierarchies correctly.\n *\n * Resolved types are cached per `TypeChecker` instance (via a `WeakMap`) so\n * repeated calls across files do not re-scan source files.\n *\n * Requires `program` in the {@link SchemaContext}.\n *\n * @param modulePathPattern Substring matched against source-file paths.\n * @param exportName The exported type, class or interface name.\n *\n * @example\n * ```typescript\n * const BaseWindow = t.fromModule(\"/electron/\", \"BaseWindow\");\n * const Buffer = t.fromModule(\"@types/node\", \"Buffer\");\n * ```\n */\n fromModule: (modulePathPattern: string, exportName: string): TypeSchema =>\n schema((type, { checker, program }) => {\n if (!program) {\n throw new Error(\n \"t.fromModule() requires `program` in the SchemaContext.\"\n );\n }\n\n let cache = moduleTypeCache.get(checker);\n if (!cache) {\n cache = new Map();\n moduleTypeCache.set(checker, cache);\n }\n\n const cacheKey = JSON.stringify([modulePathPattern, exportName]);\n\n if (!cache.has(cacheKey)) {\n let resolved: Type | null = null;\n for (const sourceFile of program.getSourceFiles()) {\n if (!sourceFile.fileName.includes(modulePathPattern)) continue;\n const moduleSymbol = checker.getSymbolAtLocation(sourceFile);\n if (!moduleSymbol) continue;\n const exportSymbol = checker\n .getExportsOfModule(moduleSymbol)\n .find((s) => s.getName() === exportName);\n if (exportSymbol) {\n resolved = checker.getDeclaredTypeOfSymbol(exportSymbol);\n break;\n }\n }\n cache.set(cacheKey, resolved);\n }\n\n const targetType = cache.get(cacheKey);\n if (targetType == null) {\n throw new Error(\n `t.fromModule(): could not resolve export \"${exportName}\" from any module matching \"${modulePathPattern}\".`\n );\n }\n return checker.isTypeAssignableTo(type, targetType);\n }),\n\n /**\n * Escape hatch for arbitrary predicates that the DSL cannot express directly.\n *\n * @example\n * ```typescript\n * const HasLengthProp = t.custom((type) =>\n * type.getProperty(\"length\") != null,\n * );\n * ```\n */\n custom: (predicate: AcceptsFn): TypeSchema => schema(predicate),\n};\n\n/**\n * Returns `true` if the given TypeScript type is assignable to the schema–in\n * other words, if `type extends T` with `T` the TypeScript type represented\n * by the schema.\n */\nexport function isAssignableTo(\n ctx: SchemaContext,\n type: Type,\n schema: TypeSchema\n): boolean {\n return schema._accepts(type, ctx);\n}\n"],"mappings":";AAEA,MAAM,2BAA2B;;;;;;;AAmBjC,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