Skip to content

TS9102: Missing Field Instance

Category: Derive
Severity: Error

Message

Cannot derive '{typeclass}' for '{type}': field '{field}' of type '{fieldType}' has no '{typeclass}' instance

Explanation

When @derive generates a typeclass instance for a composite type, all field types must also have that typeclass instance. This error occurs when one or more fields don't have the required instance.

Example

typescript
class External {
  data: unknown;
}

// Error: field 'external' of type 'External' has no 'Eq' instance
@derive(Eq)
interface MyType {
  id: number;      // ✓ number has Eq
  name: string;    // ✓ string has Eq
  external: External; // ✗ External has no Eq
}

Solutions

1. Derive for the field type

If you control the field type, add the derive there too:

typescript
@derive(Eq)
class External {
  data: unknown;
}

2. Provide a custom instance for the field type

typescript
@instance
const eqExternal: Eq<External> = {
  equals: (a, b) => a.data === b.data
};
// Access via: External.Eq

3. Use field-level annotations to skip fields

typescript
@derive(Eq)
interface MyType {
  id: number;
  name: string;
  @noDerive
  external: External; // Excluded from derivation
}

4. Provide a custom instance for the whole type

If the auto-derivation doesn't fit, write a custom instance:

typescript
@instance
const eqMyType: Eq<MyType> = {
  equals: (a, b) => a.id === b.id && a.name === b.name
};
// Access via: MyType.Eq

Debugging Tips

  1. Check if the field type is from an external library that doesn't use typesugar
  2. Look for typos in the field type name
  3. Ensure imports are correctly set up for field types from other modules

See Also

Released under the MIT License.