Upgrading from 2.2.0 -> 2.3.2: Type 'unknown' does not satisfy the constraint 'Record<never, never>'

69 views Asked by At

I am using Hilla for my project because I need a Java backend so that I can use auto-generated java library code. The library code is in another jar on the classpath. The issue I am seeing is related to the changes in this PR: https://github.com/vaadin/hilla/pull/1252

The code below worked ok with Hilla 2.2.0.

I have a library interface:

public interface HlaTimeStamped<T> {
   T getValue();
   HlaTimeStamp getTimeStamp();
   HlaLogicalTime getLogicalTime();
}

The relevant generated frontend ts is:

/* HlaTimeStamped.ts */
import type HlaLogicalTime_1 from "./HlaLogicalTime.js";
import type HlaTimeStamp_1 from "./HlaTimeStamp.js";
interface HlaTimeStamped {
    value: unknown;
    timeStamp?: HlaTimeStamp_1;
    logicalTime?: HlaLogicalTime_1;
}
export default HlaTimeStamped;

and

/* HlaTimeStampedModel.ts */
class HlaTimeStampedModel<T extends HlaTimeStamped_1 = HlaTimeStamped_1> extends ObjectModel_1<T> {
    static override createEmptyValue = makeObjectEmptyValueCreator_1(HlaTimeStampedModel);
    get value(): ObjectModel_1<unknown> { /* Type 'unknown' does not satisfy the constraint 'Record<never, never>'. */
        return this[_getPropertyModel_1]("value", (parent, key) => new ObjectModel_1(parent, key, false));
    }
    get timeStamp(): HlaTimeStampModel_1 {
        return this[_getPropertyModel_1]("timeStamp", (parent, key) => new HlaTimeStampModel_1(parent, key, true));
    }
    get logicalTime(): HlaLogicalTimeModel_1 {
        return this[_getPropertyModel_1]("logicalTime", (parent, key) => new HlaLogicalTimeModel_1(parent, key, true));
    }
}

When the application is built I get the error Type 'unknown' does not satisfy the constraint 'Record<never, never>'. from the value() function declaration.

The issue is that previously, ObjectModel is declared as

export declare class ObjectModel<T> extends AbstractModel<T>

whereas now it is

declare class ObjectModel<T extends Record<never, never> = Record<never, never>> extends AbstractModel<T> { ... }

I am stumped as how to fix this, given that all the code here is autogenerated.

1

There are 1 answers

1
Anton Platonov On BEST ANSWER

This is clearly a bug in Hilla and not with your library code. I submitted this as an issue here: https://github.com/vaadin/hilla/issues/1612

Unfortunately, generic parameters are not supported as such yet. Emitting something like unknown in TypeScript for such cases is probably the best we could offer at the moment. However, it is not expected that Hilla generates erroneous TypeScript models for this missing feature.

As a workaround, please try applying an upper bound to the generic parameter:

public interface HlaTimeStamped<T extends Entity> {
    T getValue();
    // ...
}