@orkestrel/contract 0.0.2 → 0.0.3

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.
@@ -108,7 +108,7 @@ export declare function arrayShape<S extends ContractShape>(items: S, options?:
108
108
  * })
109
109
  * ```
110
110
  */
111
- export declare function objectShape<P extends Readonly<Record<string, ContractShape>>>(properties: P, options?: ObjectShapeOptions): ObjectShape<P>;
111
+ export declare function objectShape<P extends Readonly<Record<string, ContractShape>>, const A extends boolean | ContractShape = false>(properties: P, options?: ObjectShapeOptions<A>): ObjectShape<P, A>;
112
112
  /**
113
113
  * Build an open {@link ObjectShape} with no fixed properties — a dictionary.
114
114
  *
@@ -125,7 +125,7 @@ export declare function objectShape<P extends Readonly<Record<string, ContractSh
125
125
  * const bindings = recordShape(numberShape()) // ~ Record<string, number>
126
126
  * ```
127
127
  */
128
- export declare function recordShape<S extends ContractShape>(values: S, options?: RecordShapeOptions): ObjectShape;
128
+ export declare function recordShape<S extends ContractShape>(values: S, options?: RecordShapeOptions): ObjectShape<Record<never, never>, S>;
129
129
  /**
130
130
  * Build a {@link UnionShape} from a list of variant shapes (`anyOf` in JSON Schema).
131
131
  *
@@ -209,10 +209,10 @@ export interface ArrayShape<S extends ContractShape = ContractShape> {
209
209
  * `false` rejects them (closed), `true` accepts them as-is, a `ContractShape`
210
210
  * validates them.
211
211
  */
212
- export interface ObjectShape<P extends Readonly<Record<string, ContractShape>> = Readonly<Record<string, ContractShape>>> {
212
+ export interface ObjectShape<P extends Readonly<Record<string, ContractShape>> = Readonly<Record<string, ContractShape>>, A extends boolean | ContractShape = boolean | ContractShape> {
213
213
  readonly type: 'object';
214
214
  readonly properties: P;
215
- readonly additionalProperties?: boolean | ContractShape;
215
+ readonly additionalProperties?: A;
216
216
  readonly description?: string;
217
217
  }
218
218
  /**
@@ -285,10 +285,7 @@ export type Infer<S extends ContractShape> = [ContractShape] extends [S] ? unkno
285
285
  } ? V extends readonly (infer L)[] ? L : never : S extends {
286
286
  readonly type: 'array';
287
287
  readonly items: infer I;
288
- } ? I extends ContractShape ? readonly Infer<I>[] : never : S extends {
289
- readonly type: 'object';
290
- readonly properties: infer P;
291
- } ? P extends Readonly<Record<string, ContractShape>> ? InferObject<P> : never : S extends {
288
+ } ? I extends ContractShape ? readonly Infer<I>[] : never : S extends ObjectShape<infer P, infer A> ? P extends Readonly<Record<string, ContractShape>> ? A extends boolean | ContractShape ? InferObject<P, A> : never : never : S extends {
292
289
  readonly type: 'union';
293
290
  readonly variants: infer V;
294
291
  } ? V extends readonly ContractShape[] ? InferUnion<V> : never : S extends {
@@ -300,9 +297,10 @@ export type Infer<S extends ContractShape> = [ContractShape] extends [S] ? unkno
300
297
  } ? I extends ContractShape ? Infer<I> | null : never : S extends JSONShape ? JSONValue : unknown;
301
298
  /**
302
299
  * {@link Infer} of an object shape's `properties` — the required keys, plus the
303
- * `optional`-wrapped keys as optional members.
300
+ * `optional`-wrapped keys as optional members, plus the index-signature
301
+ * contribution of `additionalProperties` (see {@link InferIndex}).
304
302
  */
305
- export type InferObject<P extends Readonly<Record<string, ContractShape>>> = Readonly<{
303
+ export type InferObject<P extends Readonly<Record<string, ContractShape>>, A extends boolean | ContractShape = false> = Readonly<{
306
304
  [K in keyof P as P[K] extends {
307
305
  readonly type: 'optional';
308
306
  } ? never : K]: Infer<P[K]>;
@@ -313,9 +311,49 @@ export type InferObject<P extends Readonly<Record<string, ContractShape>>> = Rea
313
311
  readonly type: 'optional';
314
312
  readonly inner: infer I;
315
313
  } ? I extends ContractShape ? Infer<I> : never : never;
316
- }>;
314
+ }> & ([keyof P] extends [never] ? InferIndex<A> : InferOpenIndex<A>);
315
+ /**
316
+ * The index-signature contribution of a pure record shape's `additionalProperties`
317
+ * — the `recordShape` case, where `properties` is empty.
318
+ *
319
+ * @remarks
320
+ * `false` (closed) contributes `unknown`, which collapses away in an
321
+ * intersection — a closed object's {@link Infer} is unaffected. `true` (open,
322
+ * unconstrained) contributes an `unknown`-valued index signature. A
323
+ * {@link ContractShape} (open, constrained) contributes an index signature
324
+ * typed to that shape's own `Infer` — sound here because there are no fixed
325
+ * properties for the index to collide with.
326
+ */
327
+ export type InferIndex<A extends boolean | ContractShape> = [A] extends [false] ? unknown : [A] extends [true] ? {
328
+ readonly [k: string]: unknown;
329
+ } : A extends ContractShape ? {
330
+ readonly [k: string]: Infer<A>;
331
+ } : unknown;
332
+ /**
333
+ * The index-signature contribution of a MIXED object shape's
334
+ * `additionalProperties` — one with both fixed `properties` and an open tail.
335
+ *
336
+ * @remarks
337
+ * A typed index (`{ readonly [k: string]: Infer<A> }`) collapses any
338
+ * differently-typed fixed property to `never` on intersection and makes the
339
+ * object type unconstructable — TypeScript rejects assigning any property
340
+ * whose type differs from the index value type. So when `A` is a
341
+ * {@link ContractShape} here, the index is deliberately widened to
342
+ * `{ readonly [k: string]: unknown }`: the static type stops over-claiming the
343
+ * extra-key type while the runtime guard still validates extras against `A`.
344
+ * `false` / `true` behave exactly as {@link InferIndex}.
345
+ */
346
+ export type InferOpenIndex<A extends boolean | ContractShape> = [A] extends [false] ? unknown : [A] extends [true] ? {
347
+ readonly [k: string]: unknown;
348
+ } : A extends ContractShape ? {
349
+ readonly [k: string]: unknown;
350
+ } : unknown;
317
351
  /** {@link Infer} of a union shape's `variants` — the union of each variant's inferred type. */
318
352
  export type InferUnion<V extends readonly ContractShape[]> = V extends readonly (infer U)[] ? U extends ContractShape ? Infer<U> : never : never;
353
+ /** {@link Infer} with all `readonly` modifiers stripped — for consumers writing the parsed value. */
354
+ export type InferMutable<S extends ContractShape> = {
355
+ -readonly [K in keyof Infer<S>]: Infer<S>[K];
356
+ };
319
357
  /** Options for {@link StringShape} (via `stringShape`). */
320
358
  export interface StringShapeOptions {
321
359
  readonly min?: number;
@@ -353,8 +391,8 @@ export interface ArrayShapeOptions {
353
391
  readonly description?: string;
354
392
  }
355
393
  /** Options for {@link ObjectShape} (via `objectShape`). */
356
- export interface ObjectShapeOptions {
357
- readonly additionalProperties?: boolean | ContractShape;
394
+ export interface ObjectShapeOptions<A extends boolean | ContractShape = boolean | ContractShape> {
395
+ readonly additionalProperties?: A;
358
396
  readonly description?: string;
359
397
  }
360
398
  /** Options for record shapes (via `recordShape`). */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orkestrel/contract",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Zero-dependency contract toolkit — type guards, combinators, parsers, and a shape DSL that compiles one contract shape into a guard, parser, JSON Schema, and generator. The foundation package of the @orkestrel line.",
5
5
  "keywords": [
6
6
  "contract",