-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
preferinfer
modifier for type parameters
#52241
Conversation
The TypeScript team hasn't accepted the linked issue #26242. If you can get it accepted, this PR will have a better chance of being reviewed. |
c06ba17
to
ea45dc2
Compare
…preferring inferring Co-authored-by: Wesley Wigham <[email protected]>
This is half of the story the other half is where this is worse than #26349. It's worse because now I have to rely on the callee to "allow me" to pass in partial type arguments, and why should the callee be allowed to control my ability to pass in partial type arguments or not, it's irrelevant to the callee. That is let's say there's a function But I also understand the other half where it's an improvement, eg if we have something like But we can achieve what this PR achieves with #26349 also, simply by declaring So what I'm saying is that this PR doesn't just solve a problem, it also creates another one (ie having to "ask" the callee to "allow" partial type parameter inference). But if #26349 allows (if it already doesn't) I hope I've got things right, just my two cents. |
This is my primary motivation behind this PR.
This probably isn't as easy as it sounds here because
I agree that I didn't think this part through. The bottom line for me is that some mechanism for this is needed. This is one of the most requested TS features - especially desired by library authors. I don't care much about the actual mechanism that will allow this - I could even live with I also mention in the PR description that this isn't a 1-to-1 replacement for |
I don't follow this, what would happen in case of an inference failure is what happens today, ie the type parameter gets resolved to the constraint or whatever. I'm not suggesting a new behavior, In case you're asking if But again the mechanism and behavior of what should happen when is already there in #26349, it's just about bringing a new syntactic aid ie
I couldn't find where you mention or indicate that. Moreover you also explicitly write... ...which indicates otherwise. Overall I agree there's a need for this kind of feature, but as I said it's better to extend #26349 with |
The constraint is usually the default too - but that's not always the case. You can use different values for both (within the boundaries of the allowed subtype relationship). It's not necessarily something that I'd like to do but this proposal removes such a possibility (since you use
Yeah, this is what I was getting at. I don't think that overloading the unions to add hints for the compiler would be a good syntax for this but perhaps something different could be used.
Oh, sorry! I could swear that I included such a note somewhere in the description but apparently, I didn't. It is a given though - since this doesn't preclude merging that other PR as well. As you have observed - this PR mainly fixes the problem of "internal" type parameters, which is a slightly different problem than the one solved by that other PR. I mostly copied the issue references from the other PR as the overlap is, IMHO, sufficient enough. I've seen PRs addressing some of the issues by providing alternatives etc and not necessarily by implementing the requested feature/bug fix "literally". I don't have a problem with tweaking those links to smth like "relates to" though. That's not really that important to me here 😉 |
Yeah that's a secondary issue. Another option is instead of
Haha, no problems. |
@Andarist I'm marked this as a draft since the design of partial inference was never finished. |
@sandersn that’s understandable. Is there any chance that we could revive the design process of this feature anyhow? It’s highly requested and I would love to work on the actual implementation. Perhaps as a 5.1 goal? 😉 |
@Andarist the first step in the design process is to open a bug to discuss the design. |
This is not yet complete - it's an idea I have and one that I want to have feedback on since some way to allow partial inference is highly in demand by library author and type parameter modifiers seems to be a natural fit for this.
I think that in reality, people would most likely opt into partial inference for the whole signature - but I'm not sure if you'd like to invent a new syntax-level concept just for this purpose. One alternative, with a completely different syntax, that I can imagine would be something like this:
This kind of thing could serve as a modifier for the list of type parameters.
The implementation here builds heavily on the @weswigham's work from #26349 . The mechanism to opt into partial inference is different though. The other PR required opt-in from the caller - whereas this one here requires opt-in by the declaration author. I would consider this a major DX improvement. The downside is that this PR here only allows for inferring trailing type parameters (in the partial type parameters lists) - but I think it's a reasonable limitation/a fair tradeoff.
supersedes #26349
closes #26242
closes #10571.