Skip to content

Specific instance of FormInstrument is not assignable to undifferentiated FormInstrument #897

Open

Description

@joshunrau
import type { FormInstrument } from '@opendatacapture/schemas/instrument';

type HappinessQuestionnaireData = {
  overallHappiness: number;
};

type HappinessQuestionnaire = FormInstrument<HappinessQuestionnaireData>;
type T = HappinessQuestionnaire extends FormInstrument ? true : false;
// T is false, when it should be true

Activity

  1. added
    Bug
    Priority: Medium
    Difficulty: Low
    Area: Instruments
    Good First Issue
    on Sep 27, 2026
  2. joshunrau commented on Oct 26, 2026

    @joshunrau
    CollaboratorAuthor

    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 dependency
    type InstrumentMeasuresData<TData, TAnyData> = IsEqual<TData, TAnyData> extends true ? 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: InstrumentsBugDifficulty: LowGood First IssuePriority: Medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions