Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom attribute type extraction for union types #334

Merged
merged 1 commit into from
Nov 29, 2023

Conversation

wentsul
Copy link
Contributor

@wentsul wentsul commented Nov 29, 2023

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;}

Copy link

netlify bot commented Nov 29, 2023

Deploy Preview for electrodb-dev canceled.

Name Link
🔨 Latest commit 9320d15
🔍 Latest deploy log https://app.netlify.com/sites/electrodb-dev/deploys/65676d57d33110000873e897

@tywalch
Copy link
Owner

tywalch commented Nov 29, 2023

Hey @wentsul 👋

I love you, thank you!

@wentsul
Copy link
Contributor Author

wentsul commented Nov 29, 2023

Hey @wentsul 👋

I love you, thank you!

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

@tywalch tywalch merged commit ffecb33 into tywalch:master Nov 29, 2023
4 of 8 checks passed
@wentsul wentsul deleted the fix-union-type-extraction branch November 29, 2023 17:48
@tywalch
Copy link
Owner

tywalch commented Nov 29, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants