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

Add Extends #14

Merged
merged 5 commits into from
Sep 28, 2022
Merged

Add Extends #14

merged 5 commits into from
Sep 28, 2022

Conversation

danieldietrich
Copy link
Contributor

Solves #13

@danieldietrich
Copy link
Contributor Author

danieldietrich commented Sep 27, 2022

Hi @garronej, this PR is complete, however, I marked it as draft because of the following questions:

  • is the prettier formatting of src/Extends.ts ok?
  • is the handling of never ok? (see this discussion)

TODO: I will add the missing docs https://docs.tsafe.dev/main/extends

@danieldietrich
Copy link
Contributor Author

I aligned the behavior of the type Extends to the one of the built-in extends TS functionality (see my second commit). Introducing another semantics seems odd to me and would confuse more than being helpful.

@danieldietrich
Copy link
Contributor Author

Oh boy, I've found some kind of unexpected behavior of TypeScript:

type Extends<T1, T2> = T1 extends T2 ? true : false;

type A1 = never extends any ? true : false    // = true
type B1 = Extends<never, any>                 // = never ❌

type A2 = any extends never ? true : false    // = boolean
type B2 = Extends<any, never>                 // = boolean ✅

type A3 = never extends 1 ? true : false      // = true
type B3 = Extends<never, 1>                   // = never ❌

type A4 = 1 extends never ? true : false      // = false
type B4 = Extends<1, never>                   // = false ✅

type A5 = never extends never ? true : false; // = true
type B5 = Extends<never, never>               // = never ❌

The following does not solve the problem:

type Extends<T1, T2> = T1 extends never ? true : T1 extends T2 ? true : false;

However, the following does solve the problem:

type Extends<T1, T2> = StrictEquals<T1, never> extends true ? true : T1 extends T2 ? true : false;

I will provide a fix with another commit...

@garronej
Copy link
Owner

Hi @danieldietrich,

is the prettier formatting of src/Extends.ts ok?

LGTM

is the handling of never ok? (see #13 (comment) discussion)

It's probably a bug but let's leave it since I have no time to investigate right now. It's maybe on purpose.

TODO: I will add the missing docs https://docs.tsafe.dev/main/extends

<3

I will provide a fix with another commit...

Thanks you for taking this so seriously!

@danieldietrich
Copy link
Contributor Author

Ok, now I consider the type logic to be complete. I will mark it as ready for review.

@danieldietrich danieldietrich marked this pull request as ready for review September 27, 2022 22:24
assert<Equals<Extends<A, A>, true>>();
}

{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a little remark regarding the following tests - I intentionally added duplications because it is important in the respective context to understand the whole picture. Overlapping tests do not really harm.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got you!

@garronej garronej merged commit 3a6e446 into garronej:main Sep 28, 2022
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

2 participants