dynamodb-toolbox
dynamodb-toolbox copied to clipboard
Type Inference Doesn't Play Well With Type Intersections
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.
Same problem here. No matter what I try, the map attribute type is always set to any for me
any update on this? 10 months passed after initial reporting but no response on this topic.
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.