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

Type inference on tuple intersection types broken #59521

Open
miguel-leon opened this issue Aug 2, 2024 · 0 comments
Open

Type inference on tuple intersection types broken #59521

miguel-leon opened this issue Aug 2, 2024 · 0 comments
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone

Comments

@miguel-leon
Copy link

miguel-leon commented Aug 2, 2024

πŸ”Ž Search Terms

infer tuple intersection

πŸ•— Version & Regression Information

  • Version 5.5.4

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAKghgJwOYWFAvFA2gOwK4C2ARhAgDRQDOwCAljkhUQPbMA2EcOAulAGRQA3lDjBgALio16SANxQAvrICwAKDWhIUAIIYoAJU4ATZjjYgAPPGSoAfPID0DqAHdalABYAzPGyjAPegBrGTUNcGgACWMARj1rFDQIAA9gCBwjSmx6L1IoSIoAOmKcvPhaNl4AfnyoSRwIADdSR2cTCEocAHI0F2YEIPCtaLgjACZ4xESoFLSMrKxShHyi4q4QLG5q2vqmlqgnV36gyjDVTWhytjjMBNQZ1PTM7Jxc5YKoYsKl2DgK7audSgDWaCFaUHanR6RwGQ0ufzYE1uU3usyeC3Wq2 rzKCIBCKBIP2h0h3V6xzOFwMewQlAgIyMN1 NiSj3m2C P30VwoXMi230kUJNPBpOhfVh5wi1NBdIZSOZ0zR7KwX3Wm15OOWgoFQt2oNFzA6ZJhg3UUq0hllECuTLurLmz1VJS1Bh5UD5AsB uJbSNUPJkqpVtIdKuCvtD0dC05ru5CIo6y9BJ9YIOfuN4opqiAA

πŸ’» Code

type Target = [number, string, boolean] & { att: string; };

type A = Readonly<Target>; // wishful thinking

type Head1 = Target extends [infer H, ...infer Tail] ? H : never; // doesn't work
type Head2 = Target extends [infer H, ...any[]] ? H : never; // works

type Tail1 = Target extends [infer H, ...infer Tail] ? Tail : never; // doesn't work
type Tail2 = Target extends [any, ...infer Tail] ? Tail : never; // doesn't work

type ReverseHead1 = Target extends [...infer RTail, infer RH] ? RH : never; // doesn't work
type ReverseHead2 = Target extends [...any[], infer RH] ? RH : never; // doesn't work

type ReverseTail1 = Target extends [...infer RTail, infer RH] ? RTail : never; // doesn't work
type ReverseTail2 = Target extends [...infer RTail, any] ? RTail : never; // doesn't work

πŸ™ Actual behavior

Doesn't work

πŸ™‚ Expected behavior

Works

Additional information about the issue

I would expect Readonly<[number, string, boolean] & { att: string }> to be readonly [number, string, boolean] & { readonly att: string }, but that's wishful thinking. I'm sure you guys got a myriad of reasons and design limitations that don't allow it; weak types, homomorphic, variance, instantiation and whatnot.

In any case, I thought, whatever, I'll do it myself with some infers... but it looks like I won't be able to do the general case because infers break on tuple types that are intersected.

So, just letting you know in case you care... here are some unexpected behaviors ✌️

@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases labels Aug 2, 2024
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Projects
None yet
Development

No branches or pull requests

2 participants