-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Specialize declared generic interfaces based on parameter shape #13852
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
Comments
maybe related to #12424 |
either #12424 or have some sort of overload on generic type parameters |
I wrote a post that touched on this issue and got some great feedback from the /r/javascript reddit. As it turns out, there's a workaround that already works: specialize by overloading the declare interface Chainable<T> {
value(): T; // This methods is available for any T.
// This method is only available for array types, where T matches V[].
map<U, V>(this: Chainable<V[]>, mapFn: (v: V) => U): Chainable<U[]>;
} |
Overloading on the type of |
For typing something like underscore's
_.chain
, it would be helpful if interfaces could be extended based on the shape of their type parameters.For example:
Existing type definitions try to infer whether
T
is an array, object, array of objects, etc. But this is cumbersome and they'll never be able to do it as well as TypeScript itself.The snippet above doesn't parse with TypeScript 2.1.5:
The text was updated successfully, but these errors were encountered: