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

Composite Evaluate #349

Merged
merged 5 commits into from
Mar 23, 2023
Merged

Composite Evaluate #349

merged 5 commits into from
Mar 23, 2023

Conversation

sinclairzx81
Copy link
Owner

@sinclairzx81 sinclairzx81 commented Mar 23, 2023

This PR reverts the logic on Type.Composite back to 0.25.0 Type.Intersect due to excessive type instantiation issues. On 0.26.0, Type.Composite attempted to take a union for overlapping properties, however due to the sophistication required to type map the unions for overlapping properties, this has resulted in problems for some users upgrading to 0.26.0.

As such, 0.26.2 reverts back to the 0.25.0 interpretation, but applies type mappings more inline with TS's interpretation of an overlapping varying property types. In the examples below, the type C is the evaluated type for Type.Composite. Note that TS will not union for overlapping properties and instead evaluate never. The 0.26.2 implementation falls inline with this evaluation.

{ // evaluation case 1: non-varying
  type T = { a: number } & { a: number }

  type C = {[K in keyof T]: T[K] }  // type C = { a: number }
}

{ // evaluation case 2: varying
  type T = { a: number } & { a: string }

  type C = {[K in keyof T]: T[K] }  // type C = { a: never }
}

{ // evaluation case 3: single optional
  type T = { a?: number } & { a: number }

  type C = {[K in keyof T]: T[K] }  // type C = { a: number }
}

{ // evaluation case 4: all optional
  type T = { a?: number } & { a?: number }

  type C = {[K in keyof T]: T[K] }  // type C = { a?: number | undefined }
}

Note: the Type.Composite is intended to be a temporary type which can be replaced with a more general Type.Mapped in future revisions of TypeBox. As the infrastructure to support mapped types does not exist, users can use Type.Composite to partially replicate mapped type evaluation for composited object types only.

@sinclairzx81 sinclairzx81 merged commit 1cbb5c6 into master Mar 23, 2023
@sinclairzx81 sinclairzx81 deleted the composite branch March 26, 2023 08:09
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.

None yet

1 participant