You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Specific instance of FormInstrument is not assignable to undifferentiated FormInstrument #897
Area: InstrumentsInstrument authoring API, bundling, runtime, rendering, measures and the built-in libraryBugType: existing behavior is wrongDifficulty: LowIsolated change, about 2 hours or lessGood First IssueWell-scoped, low-risk and self-contained; a good entry point for a new contributorPriority: MediumReal value; schedule normally
importtype{FormInstrument}from'@opendatacapture/schemas/instrument';typeHappinessQuestionnaireData={overallHappiness: number;};typeHappinessQuestionnaire=FormInstrument<HappinessQuestionnaireData>;typeT=HappinessQuestionnaireextendsFormInstrument ? true : false;// T is false, when it should be true
Still reproduces on main (d86740e). FormInstrument now comes from @opendatacapture/runtime-core, because @opendatacapture/schemas/instrument no longer exports it.
Cause
Only measures fails. Checked one property at a time, content, validationSchema and initialValues are assignable. TData is contravariant in two places:
ComputedInstrumentMeasure.value is a function-typed property (packages/runtime-core/src/types/instrument.base.ts:127).
ConstantInstrumentMeasure.ref is built from the keys of TData (instrument.base.ts:136-142). On its own, 'overallHappiness' is assignable to string. Inside the InstrumentMeasures alias, though, tsc compares by the variance it measured for TData, and the key mapping makes that variance contravariant.
InteractiveInstrument<{ score: number }> fails against InteractiveInstrument the same way. Concrete instruments also fail against AnyInstrument and AnyUnilingualFormInstrument. ScalarInstrument passes only because its default TData is any (instrument.base.ts:199).
Fixes that do not work
Method syntax, value(data: TData): ref still fails.
InstrumentMeasures<out TData>: tsc rejects it with TS2636, because of ref.
Defaulting TData to any: Fields<any> turns every field into DateField | DynamicField.
Fix
Erase the measures' data type only when an instrument is typed with exactly its kind's widest data. That is what the bare and Any* types mean:
// instrument.base.ts, @internal; IsEqual is from type-fest, already a runtime-core dependencytypeInstrumentMeasuresData<TData,TAnyData>=IsEqual<TData,TAnyData>extendstrue ? any : TData;
Do not write the check as [TAnyData] extends [TData]. That is also true when every data key is optional, as with z.object({ a: z.number().optional() }). Such forms would stop catching a typo in value or a bad ref, which main catches today.
instrument.form.ts:297-310: change ScalarInstrument<TData, TLanguage> & {...} to Merge<...>, the way InteractiveInstrument already does it. With &, ScalarInstrument's measures is intersected back in, so changing the existing override at :302 alone leaves the snippet false. Type that override as InstrumentMeasures<InstrumentMeasuresData<TData, FormInstrument.Data>, TLanguage> | null.
instrument.interactive.ts:17-47: add the same measures override, with InteractiveInstrument.Data.
I checked this on copies of the main types, with tsc 6.0.3 and with the TypeScript 5.4.5 bundled in the playground's Monaco. The snippet resolves to true, defineInstrument(...) satisfies FormInstrument compiles, and both existing .test-d.ts files still pass. A typo in value and a ref that is not a data key are still rejected, including with all-optional data. Under tsc 6.0.3 the Zod mirrors also type-check unchanged, because their measures are already InstrumentMeasures<any, TLanguage> (packages/schemas/src/instrument/instrument.base.ts:150).
The widening at packages/instrument-utils/src/measures.ts:24 is still needed, because FILE data is { [key: string]: never } and value(data) cannot take it. The comment above it at :22-23 blames Json, which stops being true with this fix, so reword it in the same PR.
Tests
Unit: in src/types/__tests__/instrument.form.test-d.ts, assert that FormInstrument<{ x: number }> matches FormInstrument, FormInstrument<{ x: number }, Language> matches AnyUnilingualFormInstrument, and FormInstrument<{ x: number }, Language[]> matches AnyMultilingualFormInstrument. Pass the language explicitly. With the default InstrumentLanguage, the Any* checks fail on language, tags and content whether or not the fix is in. Also assert that FormInstrument<{ a?: number }>['measures'] still equals InstrumentMeasures<{ a?: number }, InstrumentLanguage> | null, which catches the looser check. Put the interactive case in a new instrument.interactive.test-d.ts, and add that file to the type-test list in packages/runtime-core/AGENTS.md.
e2e: in testing/src/specs/playground.spec.ts, load an index.ts through generatePlaygroundURL. Use a form with a computed measure, ending in satisfies FormInstrument, plus one deliberate type error on a known line. Open Upload Bundle, which lists editor type errors (apps/playground/src/components/Header/ActionsDropdown/UploadBundleDialog.tsx:155-182), and assert that it lists exactly one error, on that line. The deliberate error proves the diagnostics ran. On main the list has two errors, so the test fails there. PlaygroundPage needs a helper to open the dialog. The actions trigger is an icon-only button with no accessible name, so give it an aria-label through t() with en and fr.
Outside authors only get this with a runtime/v1 release.
Cleanup afterwards
the measures: null spreads at apps/web/src/routes/_app/datahub/subjects/$subjectId/__tests__/graph.test.tsx:109 and .../table/__tests__/$recordId.test.tsx:73, plus the copy feat: add the instrument hub and record provenance #1807 adds in apps/web/src/routes/_app/datahub/instruments/$instrumentId/__tests__/graph.test.tsx
the casts at packages/react-core/src/components/FormContent/FormContent.stories.tsx:14, .../InstrumentSummary/InstrumentSummary.stories.tsx:31,48 and .../InstrumentOverview/InstrumentOverview.stories.tsx:19
the cast and comment at packages/instrument-utils/src/translate.ts:298-299
#1807 also edits the subject graph.test.tsx, so do the cleanup in its own PR after #1807 lands. #888 is also in instrument.form.ts, but it has a separate cause, and neither blocks the other.
Triage note written with Claude Code. Code references point at main @ d86740e.
Area: InstrumentsInstrument authoring API, bundling, runtime, rendering, measures and the built-in libraryBugType: existing behavior is wrongDifficulty: LowIsolated change, about 2 hours or lessGood First IssueWell-scoped, low-risk and self-contained; a good entry point for a new contributorPriority: MediumReal value; schedule normally
Activity
joshunrau commentedon Oct 26, 2026
Still reproduces on
main(d86740e).FormInstrumentnow comes from@opendatacapture/runtime-core, because@opendatacapture/schemas/instrumentno longer exports it.Cause
Only
measuresfails. Checked one property at a time,content,validationSchemaandinitialValuesare assignable.TDatais contravariant in two places:ComputedInstrumentMeasure.valueis a function-typed property (packages/runtime-core/src/types/instrument.base.ts:127).ConstantInstrumentMeasure.refis built from the keys ofTData(instrument.base.ts:136-142). On its own,'overallHappiness'is assignable tostring. Inside theInstrumentMeasuresalias, though, tsc compares by the variance it measured forTData, and the key mapping makes that variance contravariant.InteractiveInstrument<{ score: number }>fails againstInteractiveInstrumentthe same way. Concrete instruments also fail againstAnyInstrumentandAnyUnilingualFormInstrument.ScalarInstrumentpasses only because its defaultTDataisany(instrument.base.ts:199).Fixes that do not work
value(data: TData):refstill fails.InstrumentMeasures<out TData>: tsc rejects it with TS2636, because ofref.TDatatoany:Fields<any>turns every field intoDateField | DynamicField.Fix
Erase the measures' data type only when an instrument is typed with exactly its kind's widest data. That is what the bare and
Any*types mean:Do not write the check as
[TAnyData] extends [TData]. That is also true when every data key is optional, as withz.object({ a: z.number().optional() }). Such forms would stop catching a typo invalueor a badref, whichmaincatches today.instrument.form.ts:297-310: changeScalarInstrument<TData, TLanguage> & {...}toMerge<...>, the wayInteractiveInstrumentalready does it. With&,ScalarInstrument'smeasuresis intersected back in, so changing the existing override at:302alone leaves the snippetfalse. Type that override asInstrumentMeasures<InstrumentMeasuresData<TData, FormInstrument.Data>, TLanguage> | null.instrument.interactive.ts:17-47: add the samemeasuresoverride, withInteractiveInstrument.Data.I checked this on copies of the
maintypes, with tsc 6.0.3 and with the TypeScript 5.4.5 bundled in the playground's Monaco. The snippet resolves totrue,defineInstrument(...) satisfies FormInstrumentcompiles, and both existing.test-d.tsfiles still pass. A typo invalueand arefthat is not a data key are still rejected, including with all-optional data. Under tsc 6.0.3 the Zod mirrors also type-check unchanged, because their measures are alreadyInstrumentMeasures<any, TLanguage>(packages/schemas/src/instrument/instrument.base.ts:150).The widening at
packages/instrument-utils/src/measures.ts:24is still needed, because FILE data is{ [key: string]: never }andvalue(data)cannot take it. The comment above it at:22-23blamesJson, which stops being true with this fix, so reword it in the same PR.Tests
src/types/__tests__/instrument.form.test-d.ts, assert thatFormInstrument<{ x: number }>matchesFormInstrument,FormInstrument<{ x: number }, Language>matchesAnyUnilingualFormInstrument, andFormInstrument<{ x: number }, Language[]>matchesAnyMultilingualFormInstrument. Pass the language explicitly. With the defaultInstrumentLanguage, theAny*checks fail onlanguage,tagsandcontentwhether or not the fix is in. Also assert thatFormInstrument<{ a?: number }>['measures']still equalsInstrumentMeasures<{ a?: number }, InstrumentLanguage> | null, which catches the looser check. Put the interactive case in a newinstrument.interactive.test-d.ts, and add that file to the type-test list inpackages/runtime-core/AGENTS.md.testing/src/specs/playground.spec.ts, load anindex.tsthroughgeneratePlaygroundURL. Use a form with a computed measure, ending insatisfies FormInstrument, plus one deliberate type error on a known line. Open Upload Bundle, which lists editor type errors (apps/playground/src/components/Header/ActionsDropdown/UploadBundleDialog.tsx:155-182), and assert that it lists exactly one error, on that line. The deliberate error proves the diagnostics ran. Onmainthe list has two errors, so the test fails there.PlaygroundPageneeds a helper to open the dialog. The actions trigger is an icon-only button with no accessible name, so give it anaria-labelthrought()withenandfr.Outside authors only get this with a
runtime/v1release.Cleanup afterwards
measures: nullspreads atapps/web/src/routes/_app/datahub/subjects/$subjectId/__tests__/graph.test.tsx:109and.../table/__tests__/$recordId.test.tsx:73, plus the copy feat: add the instrument hub and record provenance #1807 adds inapps/web/src/routes/_app/datahub/instruments/$instrumentId/__tests__/graph.test.tsxpackages/react-core/src/components/FormContent/FormContent.stories.tsx:14,.../InstrumentSummary/InstrumentSummary.stories.tsx:31,48and.../InstrumentOverview/InstrumentOverview.stories.tsx:19packages/instrument-utils/src/translate.ts:298-299#1807 also edits the subject
graph.test.tsx, so do the cleanup in its own PR after #1807 lands. #888 is also ininstrument.form.ts, but it has a separate cause, and neither blocks the other.Triage note written with Claude Code. Code references point at
main@d86740e.