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
997 stars 63 forks source link

Fix custom attribute type extraction for union types #334

Closed wentsul closed 10 months ago

wentsul commented 10 months ago

Issue

const key = 'key';

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

// this is a simplified version of what EntityRecord 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 10 months ago

Deploy Preview for electrodb-dev canceled.

Name Link
Latest commit 9320d15d59d254f8ddc51398010d5d0b746f486b
Latest deploy log https://app.netlify.com/sites/electrodb-dev/deploys/65676d57d33110000873e897
tywalch commented 10 months ago

Hey @wentsul 👋

I love you, thank you!

wentsul commented 10 months ago

Hey @wentsul 👋

I love you, thank you!

ha, of course. @adriancooney provided me with insight on the solution.

tywalch commented 10 months ago

This has been deployed as 2.12.1, let me know if you have an issues 👍