Skip to content
New issue

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

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

Already on GitHub? # to your account

How is assignability b/w two uninstantiated generics checked? #60815

Open
1 task done
juhort opened this issue Dec 19, 2024 · 1 comment
Open
1 task done

How is assignability b/w two uninstantiated generics checked? #60815

juhort opened this issue Dec 19, 2024 · 1 comment

Comments

@juhort
Copy link

juhort commented Dec 19, 2024

Acknowledgement

  • I acknowledge that issues using this template may be closed without further explanation at the maintainer's discretion.

Comment

I have the following AllKeys type that returns all the keys of union type.

type AllKeys<T> = T extends unknown ? keyof T: never;

Now, I'm unsure why keyof T is not assignable to AllKeys<T>, whereas AllKeys<T> is assignable to keyof T. Shouldn't the opposite of this be true?

type AllKeys<T> = T extends unknown ? keyof T: never;

// KeysOfUnion<T> should not be assignable to keyof T
type WithKeyOfConstraint<T, K extends keyof T> = unknown;
type Test1<T> = WithKeyOfConstraint<T, AllKeys<T>>; // should error but doesn't

// keyof T should be assignable to KeysOfUnion<T>
type WithAllKeysConstraint<T, K extends AllKeys<T>> = unknown;
type Test2<T> = WithAllKeysConstraint<T, keyof T>; // shouldn't error but does

Playground

Would love to understand how is assignability b/w two uninstantiated generics checked?

@Andarist
Copy link
Contributor

Andarist commented Dec 19, 2024

When it comes to this specific case you can read around the comment I touched here: 69e4dcf . In general, the containing function is responsible for a lot of the assignability rules so if you are interested in how TS breaks down through the relationships you can read about it there.

That said, this PR wasn't correct and I already have closed it 😅

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
2 participants