-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Generic type parameter problem/limitation/question #4967
Comments
from a cursory look, it seems that export interface Iterable<K, V> {
flatMap(
mapper: (value?: V, key?: K, iter?: this) => this,
context?: any
): this;
} this way no need to infer any new type parameters, just pass them through, which seems to be the expected behavior of flatMap if understand the documentation correctly.
|
@myitcv please reopen if this does not address the issue. |
@mhegazy apologies, somehow I totally missed your reply! Yes #4910 does help for certain functions. However for export interface Iterable<K, V> {
map<M>(
mapper: (value?: V, key?: K, iter?: this) => M,
context?: any
): Iterable<K, M>; // can't use 'this' here
} Any thoughts? |
No, you're not missing something obvious. Typescript doesn't support higher-kinded types. Code generation might be the answer if you can't express the concept any other way. Haskell's The type is function fmap<F extends Iterable, A, B>(mapper: (a:A) => B, i: F<A>): F<B> {
// code that uses the iterable interface to iterate over i...
} Unfortunately, Typescript doesn't support higher-order types, so you'll have to find a different way of expressing this code. |
@sandersn - thanks for confirming. Code generation is probably the cleanest solution here. |
Unfortunately this means no Functor |
Consider the following which is a modified cut-down version of the
immutable-js
type definition:The existence of the type parameter
T
for the definitions ofIIterable
andISeq
allowsslice()
to reflect that it returns a sliced instance of the same container 'type' without requiring a type assertion:However I can't type
flatMap
andflip
. Taking flip as an example, it should return a container of the same 'type' but withK
andV
switched.flatMap
should return a container of the same 'type' but withMK
andMV
.This problem would be 'solved' if we could refer to generic types without requiring that they be passed arguments:
Am I missing something obvious here?
Or do I need to look to some higher order definition with code generation of (generic) interfaces to properly support this?
The text was updated successfully, but these errors were encountered: