Export BaseContext interface for strongly-typed loadContext() in _.context.ts files#1822
Export BaseContext interface for strongly-typed loadContext() in _.context.ts files#1822
Conversation
pmcelhaney
left a comment
There was a problem hiding this comment.
Create a BaseContext type and have the generated Context class extend that.
|
@copilot try again |
|
@copilot try again |
Done in c259be8. Replaced |
|
@copilot BaseContext needs to be an interface, not a class. It will not have an implementation. It just needs to define the signature of the constructor. |
Done in 8579779. |
pmcelhaney
left a comment
There was a problem hiding this comment.
Rename scenario-context.ts to _.context.ts
Done in d6a7. The generated file is now |
Summary
_.context.tsconstructors receive{ loadContext, readJson }from the module loader with no type information, makingloadContexteffectively untyped. This exports aBaseContextinterface from the generatedtypes/_.context.tsso users can implement it in their_.context.tsfiles and get the same strongly-typedloadContextoverloads already present onApplyContext— with no runtime overhead and no implementation to inherit.Original Prompt
_.context.tsconstructors receive{ loadContext, readJson }from the module loader with no type information, makingloadContexteffectively untyped. Export a type from the generated types file so users can annotate their constructors and get strongly-typedloadContextoverloads.Manual acceptance tests
types/_.context.tscontainsexport interface BaseContextwithreadonly loadContexttyped asApplyContext["loadContext"]andreadonly readJsonroutes/_.context.tsimportsBaseContextand declaresexport class Context implements BaseContextwith a constructor that initializesloadContextandreadJson_.context.tsthat implementsBaseContext, TypeScript provides typed overloads forthis.loadContext(e.g.this.loadContext("/pets")resolves toPetsContextwhen aroutes/pets/_.context.tsexists)_.context.tsfiles that do not implementBaseContextcontinue to work unchangedScenario,ApplyContext, and existingloadContextoverloads intypes/_.context.tsare unaffectedTasks
generate.ts— Exports a pureBaseContextinterface frombuildApplyContextContent; the generated file is now written totypes/_.context.ts(renamed fromtypes/scenario-context.ts):loadContextis typed asApplyContext["loadContext"]so all strongly-typed overloads (generated per discovered_.context.tsfiles) flow through automatically. No implementation — purely a structural contract.repository.ts— Updated the default_.context.tsscaffold toimport type { BaseContext } from "../types/_.context.js"and declareexport class Context implements BaseContextwith a constructor that assigns the interface's fields.generate.test.ts— Updated assertions to verifyexport interface BaseContext {and its members appear in the generatedtypes/_.context.tsoutput.context-registry.ts— Updated a code comment that referenced the oldscenario-contextname.docs/features/repl.md— Updated import examples and prose to referencetypes/_.context.ts/types/_.context.js..changeset/strongly-typed-load-context.md— Minor bump changeset updated to reference the new filename.