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(client): enhance type safety of the AtLeast utility type #24056

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

de-novo
Copy link

@de-novo de-novo commented May 1, 2024

This pull request proposes improvements to the AtLeast utility type, as discussed in issue #24055 . The changes aim to address the issues highlighted in issue #20619 , specifically focusing on enhancing type inference accuracy and ensuring greater compatibility with stricter type checks. This PR introduces a refined type definition to mitigate problems related to type intersections and to bolster type safety within the Prisma codebase.

Improve type inference accuracy by refining the AtLeast type definition, ensuring more reliable type safety across the Prisma codebase. This addresses issues with type intersection and extends compatibility with stricter type checks.
@de-novo de-novo requested a review from a team as a code owner May 1, 2024 12:40
@de-novo de-novo requested review from jkomyno and removed request for a team May 1, 2024 12:40
@CLAassistant
Copy link

CLAassistant commented May 1, 2024

CLA assistant check
All committers have signed the CLA.

Copy link

codspeed-hq bot commented May 1, 2024

CodSpeed Performance Report

Merging #24056 will not alter performance

Comparing de-novo:fix/client/AtLeast (1e31c2f) with main (70f363e)

Summary

✅ 3 untouched benchmarks

@SevInf
Copy link
Contributor

SevInf commented May 2, 2024

Hi @de-novo!
Could you please add a test for you change, which would demonstrate AtLeast not working correctly before your changes. Either disallowing something that should be allowed or allowing something that should not be. Thank you!

@de-novo
Copy link
Author

de-novo commented May 2, 2024

Hello @SevInf,

Thank you for your feedback.

The current implementation of the AtLeast type generally functions well, yet the type error reported in issue #20619:
259407674-08a702da-2ddc-4f00-ba07-af6cb97d3373

'Property ‘id’ is optional in type ‘SupplierWhereUniqueInput’ but required in type ‘{ id: string | number; code: string | number; }’,

has indeed led to some confusion during the development phase. To address this, I have opened issue #24055, proposing a change in the type definition from { id: string | number; code: string | number; } to { id: number; code: string; }, which should greatly assist in development.

While type inference in both runtime and development environments is currently functioning correctly, the changes I've made were aimed at improving the type definitions. Given these improvements are more about refinement than fixing overt issues, I am unsure how to add tests that could effectively demonstrate the previous shortcomings of the AtLeast type. Could you provide specific scenarios or examples of the type of issues you would like these tests to address?

Thank you for your assistance.

@de-novo
Copy link
Author

de-novo commented May 2, 2024

Additionally, the type inference improvements suggested in issue #24055 could also benefit libraries like Typia, which utilize TypeScript types for validation. Currently, such libraries are unable to properly recognize the proposed types, rendering them unusable. However, it is expected that these improvements will resolve this issue.

@de-novo
Copy link
Author

de-novo commented May 2, 2024

As mentioned earlier, since it's unclear how to approach adding tests that effectively demonstrate the previous shortcomings of the AtLeast type, I will provide four examples to illustrate this point.

suggested - prisma_fork
current - prisma

schema.prisma

schema

Ex 1

ex1_com_empl_str

스크린샷 2024-05-03 오전 6 50 22
// suggested 
Type 'string' is not assignable to type 'UserCompanyIdEmployeeNumberCompoundUniqueInput'.

// current 
Type 'string' is not assignable to type 'UserCompanyIdEmployeeNumberCompoundUniqueInput | (string & UserCompanyIdEmployeeNumberCompoundUniqueInput) | undefined'.

Ex 2

ex2
스크린샷 2024-05-03 오전 6 46 16

// suggested 
Type '{}' is missing the following properties from type 'UserCompanyIdEmployeeNumberCompoundUniqueInput': companyId, employeeNumber

// current 
Type '{}' is not assignable to type 'UserCompanyIdEmployeeNumberCompoundUniqueInput | (string & UserCompanyIdEmployeeNumberCompoundUniqueInput) | undefined'.

Ex 3

ex3

스크린샷 2024-05-03 오전 6 47 39
// suggested 
Property 'employeeNumber' is missing in type '{ companyId: string; }' but required in type 'UserCompanyIdEmployeeNumberCompoundUniqueInput'.

// current 
Type '{ companyId: string; }' is not assignable to type 'UserCompanyIdEmployeeNumberCompoundUniqueInput | (string & UserCompanyIdEmployeeNumberCompoundUniqueInput) | undefined'.
  Type '{ companyId: string; }' is not assignable to type 'string & UserCompanyIdEmployeeNumberCompoundUniqueInput'.
    Type '{ companyId: string; }' is not assignable to type 'string'.

Ex 4

ex4
스크린샷 2024-05-03 오전 6 49 26

// suggested 
Type '{}' is not assignable to type 'string'.

// current 
Type '{}' is not assignable to type 'string | (UserCompanyIdEmployeeNumberCompoundUniqueInput & string) | undefined'.

By aligning the type definitions more closely with expected data formats, we can prevent these common mistakes and make the codebase more intuitive for developers. This should ultimately facilitate a smoother development process and reduce potential runtime errors.

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

3 participants