dynamodb-toolbox icon indicating copy to clipboard operation
dynamodb-toolbox copied to clipboard

Type Inference Doesn't Play Well With Type Intersections

Open shellscape opened this issue 2 years ago • 2 comments

As the topic suggests, when using the EntityItem type inference utility type, the result type is munged badly when used with a type Intersection. Consider the following:

const Event = new Entity({
  attributes: {
    createdAt: { default: () => new Date().toISOString(), onUpdate: false, type: 'number' },
    data: { type: 'map' },
    id: { default: () => nanoid(), partitionKey: true, type: 'string' },
    sequence: { required: true, type: 'bigint' }
  },
  name: 'Event',
  table,
  timestamps: false
} as const);

export type EventEntity = EntityItem<typeof Event>;

The resulting type of EventEntity is correct:

type EventEntity = {
    createdAt?: number | undefined;
    data?: any;
    id: string;
    sequence: bigint;
    entity: string;
}

However, when using a type Intersection, the EventEntity type is badly distorted:

export type EventData = Record<string | number, unknown> | unknown[] | string;
export type EventEntity = EntityItem<typeof Event> & { data: EventData };
type EventEntity = {
    data: EventData;
} & InferEntityItem<Entity<"Event", undefined, undefined, Table<string, "id", Key | null>, true, true, false, "created", "modified", "entity", false, {
    readonly createdAt: {
        ...;
    };
    readonly data: {
        ...;
    };
    readonly id: {
        ...;
    };
    readonly sequence: {
        ...;
    };
}, ... 4 more ..., {
    ...;
}>, {
    ...;
}, ParseAttributes<...>, {
    ...;
}>

I haven't had time to look into what the cause might be, but I wanted to document it here.

shellscape avatar Feb 22 '23 17:02 shellscape

Same problem here. No matter what I try, the map attribute type is always set to any for me

rdzidziguri avatar Apr 23 '23 19:04 rdzidziguri

any update on this? 10 months passed after initial reporting but no response on this topic.

rdzidziguri avatar Nov 28 '23 13:11 rdzidziguri

Currently this is not planned, as v0's type system is pretty complex to optimize for this use case at the moment.

Instead I encourage using Overlays.

naorpeled avatar May 04 '24 20:05 naorpeled