Allow skipping some generics when calling a function with multiple generics
See original GitHub issueRight now in TypeScript it’s all or nothing when calling generic methods. You can either skip typing all the generics altogether and they will be inferred from the context (if possible), or you have to manually (re)define all of them. But the reality isn’t black and white, there are also shades of gray, where we can infer types for some of the generic parameters, but not others. Currently those have to be unnecessarily verbose by forcing the programmer to explicitly restate them.
Take a look at these 3 cases:
Case 1 - everything can be inferred - no need to call the method with <>
definition:
function case1<A, B, C>(a: A, b: B, c: C): A {}
example(1, '2', true);
Compiler knows that: A is number B is string C is boolean
Case 2 - nothing can be inferred, so we need to state what A, B and C should be, otherwise they’ll default to {}
:
function case2<A, B, C>(): A {}
example<number, string, boolean>();
Case 3 - the one that’s interesting to this feature request - some can be inferred, some can’t:
function case3<A, B, C>(b: string, c: boolean): A {}
// incorrect type of A - left unspecified:
example('thing');
// correct, but unnecessarily verbose - we already know that 'thing' is a string and true is a bool
example<number, string, boolean>('thing', true);
Now, typing string, boolean
in the above example isn’t a big deal, but with complex scenarios, say with a method using 5 generics, where you can infer 4 of them, retyping them all seems overly verbose and prone to error.
It would be great if we could have some way to skip re-typing the types that can automatically be inferred. Something like a special auto
or inferred
type, so we could write:
example<number, auto, auto>('thing', bool);
Or maybe even, if we only want to specify those up to a certain point:
example<number>('thing', bool);
The above “short-hand” notation could perhaps be different to account for function overloads with different number of generics.
Having such a feature would solve newcomers encountering problems such as this one: http://stackoverflow.com/questions/38687965/typescript-generics-argument-type-inference/38688143
Issue Analytics
- State:
- Created 7 years ago
- Reactions:582
- Comments:70 (19 by maintainers)
Top GitHub Comments
I can’t tell you how many times I’ve needed this feature and had to do something super ugly to dodge the non-existence of it (e.g., creating a wrapper function that has a subset of the generic types).
@lukescott
I think what you suggested would be too much of a break from the current way it works (i.e. you must specify all generics or none, no middle ground).
But as a solution I loved the suggestion about using the
= infer
syntax as a default value. I think the resulting syntax would be super clean.Additionally this could provide some protection if the value could not be inferred (rather than the generic defaulting to
any
which is probably never what you want).Another thought I’d had was an always inferred generic using
infer S
syntax, that would actually stop you setting it manually. I might be trying to push this too far now, but I think it’d round out this set of functionality nicely.