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

feat: Add Type.Assert HKT that implements behavior of TypeScript as assertions, and add Type.Cast improvements #84

Merged
merged 4 commits into from
Apr 18, 2024

Conversation

MajorLift
Copy link
Collaborator

@MajorLift MajorLift commented Mar 27, 2024

  • Adds new HKT Type.Assert, which performs type casting to related subtypes and supertypes, but returns never for impossible coercions.
  • Type._$cast is now non-distributive for union input params.
    • No functional change, but makes the intent of the implementation a bit clearer.
    • Rough proof of equivalence:
1. CastDistributive<T, U> = T extends U ? T : U
CastDistributive<A | B, U>
<=> | (A extends U ? A : U)
    | (B extends U ? B : U)

if (A extends U && B extends U)
  <=> A | B

if (A extends U && B not extends U)
  <=> A | U
  <=> U

if (A not extends U && B not extends U)
  <=> U | U
  <=> U

2. CastNonDistributive<T, U> = [T] extends [U] ? T : U
CastNonDistributive<A | B, U>
<=> [A | B] extends [U] ? A | B : U

if (A extends U && B extends U)
  <=> A | B

if (A extends U && B not extends U)
  => A | B not extends U
   U

if (A not extends U && B not extends U)
  => A | B not extends U
   U

@MajorLift MajorLift added the enhancement New feature or request label Mar 27, 2024
@MajorLift MajorLift self-assigned this Mar 27, 2024
@MajorLift MajorLift force-pushed the jongsun/240326-cast-improvements branch from 525b83e to ebc78e3 Compare March 27, 2024 04:12
@MajorLift MajorLift marked this pull request as draft March 28, 2024 15:00
@MajorLift MajorLift marked this pull request as ready for review March 28, 2024 16:20
@MajorLift MajorLift force-pushed the jongsun/240326-cast-improvements branch from ebc78e3 to c8be88a Compare March 28, 2024 23:08
@poteat
Copy link
Owner

poteat commented Apr 18, 2024

Sensible update IMO - although I wasn't fully convinced that T extends U ? T : U is equivalent to [T] extends [U] ? T : U - to my knowledge there existed a pair ($T$, $U$) such that the equivalence did not hold true - but I wasn't able to prove this, and all the tests pass.

@poteat poteat merged commit 8b52fa1 into main Apr 18, 2024
2 checks passed
@MajorLift
Copy link
Collaborator Author

Maybe you were thinking of T extends never vs. [T] extends [never]? It's true that those are not equivalent statements, but T extends never ? T : never and [T] extends [never] ? T : never should both always evaluate to never.

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

Successfully merging this pull request may close these issues.

2 participants