Repository navigation
Function parameters and return type inference with infer keyword #38182
Description
Activity
This is a great write-up, thank you!
I've been fighting with the infer typing for longer than I'd care to admit around this. Many of the js libraries out there provide something like an options object where the options are 1) highly constrained and 2) constrain the resulting properties of the returned function. I admit that I've been fighting with it because it feels like it ought to be possible with the tools available today AND it feels like it ought to be able to be incredibly succinct.
But I still haven't ruled out that I'm just not smart enough to have figured it out already.
Consider
const options = [ {name: "first", max: 500, label: "primary"}, {name: "second", code: 2, label: "secondary"} ];..with the expectation that by changing the configuration above, you can then have an initializer and painlessly infer something like the signature below, where "primary" is known by ts to be on the object.
initiateFn(options); initialized.primary(386) // known to return {first: { min: 0, max: 500, value: number }, primary: /*alias to first*/}You know that
optionswill contain all the correct constraints, but that you want all the types to be inferred so that changes to the code result in automatic type checking to check that all code will work for the given configuration.It can be especially frustrating when trying to infer a tuple or a tuple-like object.
const sizes = [{size:1,label:"small"},{size:2,label:"medium"},{size:3,label:"large"}] as const;gets you so very close (as does the variant [obj as const, obj2 as const ...], but breaks down as soon as you want to change a parameter name.label:"small"|"medium"|"large"won't get inferred appropriately when trying to have an inferred map type,sizes.map(s=>{[s.label]: myObject}), with no good inference workarounds for constrained key-name sets.Reacted by Michael Borislav- addedIn DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript
on May 11, 2020 RyanCavanaugh commented
on May 11, 2020 MemberMore actionsAnders Hejlsberg (@ahejlsberg) thoughts?
Reacted by Pawel Badenski- added a commit that references this issue
on Jun 13, 2024 - added 4 commits that reference this issue
on Aug 3, 2024
I saw a couple of similar issues, but it all provided very poor specification of the problem, so I decided to open my own issue.
Description
The further step in bringing nice fp declarations into TypeScript is adding function parameters and return type inference through
inferkeyword.Lets look at some examples:
Here we passed parameter
aas1and expect:Tto be inferred as1Pto be inferred asT->1Rto be inferred asT->1[P, R]->[1, 1]Here we passed parameter
aas[1, 2, 3]and expect:T1to be inferred as[1, 2, 3]T2to be replaced withunknowntype because we provided no info to be able to infer itP2to be inferred asT2->unknownP1to be inferred asReverse<T1>->Reverse<[1, 2, 3]>->[3, 2, 1]Rto be inferred as[T1, T2]->[[1, 2, 3], unknown][P1, P2, R]-> `[[3, 2, 1], unknown, [[1, 2, 3], unknown]]Here we expect:
Tto be inferred as[1, 2, 3]T['length']->[1, 2, 3]['length']->3LEN->3Case of wrapping type checkers:
Use cases
In other similar issues I saw only use cases of function parameters inference, so I'll extend it with return type inference cases.
The first example is a
callfunction:Also it may be used to infer return types on each step of computing
pipeandflowfunction versions with unlimited parameters count. I'm sure, there're much more cases where this feature may be applied, but I can't remember any more right now.Util types used