-
Notifications
You must be signed in to change notification settings - Fork 0
fix!: let a consumer emitting declarations compile against the seal #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| --- | ||
| "@btravstack/entity": minor | ||
| --- | ||
|
|
||
| Fix: a downstream library that emits its own declarations can now use the | ||
| package at all. | ||
|
|
||
| `class X extends Entity("X")(...)` failed with `TS4020: 'extends' clause of | ||
| exported class has or is using private name 'CtorKey'` (and `'BaseInstance'`) | ||
| for any consumer compiling with `declaration: true` — which is every published | ||
| TypeScript library. The package's own build never surfaced it, because its | ||
| `tsc` pass is `noEmit`. | ||
|
|
||
| The construction seal now uses an exported-but-unconstructable | ||
| `ConstructionKey` instead of a module-private `unique symbol`: a `unique | ||
| symbol` in computed-key position cannot be named across a module boundary even | ||
| when exported, while an ordinary property whose _type_ is an exported class | ||
| can. `ConstructionKey`, `Sealed` and `BaseInstance` are exported as types so | ||
| the emitted declarations can reference them; none is constructible and none is | ||
| meant to be used directly. | ||
|
|
||
| The seal is unchanged in strength — `new SomeEntity(...)` is still a compile | ||
| error, and `ConstructionKey` cannot be forged structurally. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /** | ||
| * A stand-in for a downstream *library*: it exports an entity subclass **and** | ||
| * emits its own declarations. That combination is what regressed before — a | ||
| * module-private `unique symbol` in the seal made every such consumer fail | ||
| * with TS4020, while this repo's own build (which does not emit declarations | ||
| * through `tsc`) stayed green. | ||
| * | ||
| * `tsconfig.consumer.json` compiles this, and both of its overrides are load | ||
| * bearing: it turns `noEmit` off because declaration emit is what surfaces a | ||
| * leaked private name, and points `paths` at `dist/*.d.mts` so this exercises | ||
| * the published types rather than `src`. | ||
| */ | ||
| import { Entity, computed } from "@btravstack/entity"; | ||
| import type { ConstructionKey } from "@btravstack/entity"; | ||
| import { z } from "zod"; | ||
|
|
||
| const OrgId = z.uuid().brand("OrgId"); | ||
| const Slug = z.string().min(1).brand("Slug"); | ||
| const Upper = z.string().min(1).brand("Upper"); | ||
|
|
||
| export class Organization extends Entity("Organization")( | ||
| { id: OrgId, slug: Slug }, | ||
| { | ||
| immutable: ["id"], | ||
| computed: { shout: computed(Upper, (d) => d.slug.toUpperCase() as z.infer<typeof Upper>) }, | ||
| }, | ||
| ) {} | ||
|
|
||
| /** The statics must still yield the subclass, not the structural base. */ | ||
| export const load = (raw: unknown): Organization => Organization.make(raw).getOrThrow(); | ||
|
|
||
| /** Nesting must still work from outside the package. */ | ||
| export const Aggregate = z.object({ owner: Organization.instance }); | ||
|
|
||
| // @ts-expect-error construction stays sealed for a consumer | ||
| new Organization({ id: "x" as never, slug: "y" as never }); | ||
|
|
||
| // @ts-expect-error the construction key cannot be forged structurally | ||
| const forged: ConstructionKey = {} as { seal: never }; | ||
| void forged; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| export { Entity, type CreateInput, type Input, type Output, type Patch } from "./entity.js"; | ||
| export { computed, type ComputedField } from "./computed.js"; | ||
| export { InvalidEntity } from "./errors.js"; | ||
| // Exported only so a consumer's emitted declarations can name them — none of | ||
| // the three is part of the API you write against. See `Sealed` in types.ts. | ||
| export type { BaseInstance, ConstructionKey, Sealed } from "./types.js"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "extends": "@btravstack/tsconfig/base.json", | ||
| "compilerOptions": { | ||
| "noEmit": false, | ||
| "declaration": true, | ||
| "declarationMap": false, | ||
| "emitDeclarationOnly": true, | ||
| "outDir": "./node_modules/.consumer-check", | ||
| "rootDir": "./consumer", | ||
| "types": ["node"], | ||
| "paths": { "@btravstack/entity": ["./dist/index.d.mts"] } | ||
| }, | ||
| "include": ["consumer/**/*"] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.