Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ export const DEFERRED_OPS = ["reset_doc"] as const;
*/
export const BATCHABLE_OPS = ["add_node", "connect", "disconnect", "set_widget", "delete_node"] as const;

/** Every kind the vocabulary defines — implemented ({@link Op}) plus deferred ({@link DeferredOp}). */
export type OpKind = WireOp["op"];

/** A kind `applyOps` implements. */
export type FrozenOpKind = (typeof FROZEN_OPS)[number];

Expand Down Expand Up @@ -478,18 +475,20 @@ export type WireOp = Op | DeferredOp;
// ---------------------------------------------------------------------------

type Equals<A, B> = [A] extends [B] ? ([B] extends [A] ? true : false) : false;
type Assert<T extends true> = T;

/** `FROZEN_OPS` names exactly the kinds `applyOps` implements. */
type _FrozenIsExactlyTheImplementedUnion = Assert<Equals<FrozenOpKind, Op["op"]>>;
/** `DEFERRED_OPS` names exactly the kinds declared-but-not-implemented. */
type _DeferredIsExactlyTheDeferredUnion = Assert<Equals<DeferredOpKind, DeferredOp["op"]>>;
/** Every declared kind is exactly once in FROZEN_OPS or DEFERRED_OPS. */
type _OpKindsArePartitioned = Assert<Equals<FrozenOpKind | DeferredOpKind, OpKind>>;
/** No kind is both implemented and deferred. */
type _FrozenAndDeferredAreDisjoint = Assert<Equals<FrozenOpKind & DeferredOpKind, never>>;
/** Batchable kinds are a subset of the implemented kinds. */
type _BatchableIsSubsetOfFrozen = Assert<Equals<Exclude<BatchableOpKind, FrozenOpKind>, never>>;
type Checked<T, Checks extends readonly true[]> = T &
(Checks[number] extends true ? unknown : never);

/** Every kind the vocabulary defines — implemented ({@link Op}) plus deferred ({@link DeferredOp}). */
export type OpKind = Checked<
WireOp["op"],
[
Equals<FrozenOpKind, Op["op"]>,
Equals<DeferredOpKind, DeferredOp["op"]>,
Equals<FrozenOpKind | DeferredOpKind, WireOp["op"]>,
Equals<FrozenOpKind & DeferredOpKind, never>,
Equals<Exclude<BatchableOpKind, FrozenOpKind>, never>,
]
>;

// ---------------------------------------------------------------------------
// Widget catalog (pinned object_info projection)
Expand Down
2 changes: 1 addition & 1 deletion test/type-negatives.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const negatives = join(root, "test", "types", "invalid-states.negative.ts");
* tests green. An exact count also means ADDING a state without updating this
* constant fails, which is the reminder you want.
*/
const EXPECTED_DIRECTIVES = 13;
const EXPECTED_DIRECTIVES = 14;
const DIRECTIVE = /^[ \t]*\/\/ @ts-expect-error\b/gm;

describe("invalid op states are unrepresentable (issue #17)", () => {
Expand Down
8 changes: 8 additions & 0 deletions test/types/invalid-states.negative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import type {
GrowConnectOp,
InteriorSetWidgetOp,
Op,
OpKind,
ResetDocOp,
SetWidgetOp,
TopLevelSetWidgetOp,
Expand Down Expand Up @@ -196,6 +197,13 @@ declare function applyOpsSignature(ops: Op[]): void;
// @ts-expect-error #17: the applier cannot be handed an op it always refuses.
applyOpsSignature([reset]);

// The public vocabulary projection accepts every declared kind and no value
// outside the wire union. This is an external-consumer check through index.ts.
const okImplementedKind: OpKind = "add_node";
const okDeferredKind: OpKind = "reset_doc";
// @ts-expect-error #21: undeclared operation kinds are not public OpKind values.
const unknownKind: OpKind = "unknown_op";

// ---------------------------------------------------------------------------
// Positive controls — these MUST compile, or the gate above is vacuous
// ---------------------------------------------------------------------------
Expand Down