tywalch / electrodb

A DynamoDB library to ease the use of modeling complex hierarchical relationships and implementing a Single Table Design while keeping your query code readable.
MIT License
956 stars 58 forks source link

Fix custom attribute type extraction for union types with RecordItem #346

Closed wentsul closed 5 months ago

wentsul commented 5 months ago

Issue

Same issue as #334 but for the EntityItem type. Copied description below:

const key = 'key';

type CustomAttributeTypeName<T> = { [key]: T };

// this is a simplified version of what EntityItem will return for
// the union type
type T1 =
  | CustomAttributeTypeName<{ prop1: string; prop2: string; }>
  | CustomAttributeTypeName<{ prop1: string }>
  | CustomAttributeTypeName<{ prop3: string }>;

// This doesn't work. `{ deploymentId: string; projectId: string; }` is missing from union.
// subtypes for a union items seem to get collapsed.
type T2 = T1 extends CustomAttributeTypeName<infer T> ? T : never;
// This works. underlying union types perserved.
type T3 = T1 extends infer U ? U extends CustomAttributeTypeName<infer U> ? U : never : never;

without the outer unboxing of the union type (the outer infer), TS collapses some of the underlying union types.

TS playground example

Before T2 returns

{prop1: string;} | {prop3: string;}

After T3 returns

{prop1: string;} | {prop1: string; prop2: string;} | {prop3: string;}
netlify[bot] commented 5 months ago

Deploy Preview for electrodb-dev canceled.

Name Link
Latest commit 86726621ef9d20d8bdf321d941df6398a92955c7
Latest deploy log https://app.netlify.com/sites/electrodb-dev/deploys/65af1bd45178ba000898c001