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

Constrained type param with keyof seems to have inconsistent behavior #58223

Closed
Phryxia opened this issue Apr 17, 2024 · 2 comments
Closed

Constrained type param with keyof seems to have inconsistent behavior #58223

Phryxia opened this issue Apr 17, 2024 · 2 comments

Comments

@Phryxia
Copy link

Phryxia commented Apr 17, 2024

🔎 Search Terms

generic, keyof, constrained, mapped type, conditional type

🕗 Version & Regression Information

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.4.5#code/C4TwDgpgBAYgPAFQDRQNID4oF41QgD2AgDsATAZygGsIQB7AMygSgH5mBtVAXT0JIpRywAE4BLYgHM2UUQFdoALigMAhgBtySqMQgA3CCIBQoSFADiiFKj5EylGvSYJMOBF14E7g4eKkz5bTVNCCMTcGgWHABvKHxlYjkAWwAjQxQQZV8JaQBfMNNoGGBsWCtqWkZmTAB6GqgUujp1CFVicLNzEpxLZAqnaqg6lQ0tIA

💻 Code

type F<T, K> = K extends keyof T ? T[K] extends string ? true : false : never
type G<T, K extends keyof T> = T[K] extends string ? true : false

type T = { x: number, y: string }

type Ft = F<T, keyof T> // boolean
type Gt = G<T, keyof T> // false

🙁 Actual behavior

Ft is boolean while Gt is false

🙂 Expected behavior

I'm not sure, in my opinion the first one (Ft) seems reasonable. But one thing is clear. Their result should be same.

Additional information about the issue

I suspected G<T, K> doesn't count external condition. I think G<T, K> just consider T[keyof T] undecidable, so that just fallb back to falsy type. But I'm not sure why it didn't happend with unconstrained one(F<T, K>)- which conditionally branched.

@MartinJohns
Copy link
Contributor

MartinJohns commented Apr 17, 2024

This is working as intended.

The first is a distributive conditional type. Check the documentation about conditional types. This is unrelated to keyof.

In your G case T[K] results in a union of all property types, so string | number, and this does not extend string, so you end up with false.

In your F case you have a distributive conditional type over K, so you run the conditional type for each property type and end up with a union. So basically you have (string extends string ? true : false) | (number extends string ? true : false). This type resolves to true | false and then is simplified to boolean.

@Phryxia
Copy link
Author

Phryxia commented Apr 17, 2024

@MartinJohns

Thank you for your reply. I think I misunderstood some concept of distributive conditional logic. I'll check it out once more. I'll close this as completed.

@Phryxia Phryxia closed this as completed Apr 17, 2024
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

No branches or pull requests

2 participants