Reputation: 1113
Given a generic type:
export interface Mappable<Type, Src> {
map: <T = Type, D = Src>(f: (a: Src) => D) => T;
}
export type ComposeMapper<T extends Mappable<A, B>, A, B> = (f: (a: B) => B, m: T) => T;
Why can't I use it in a declaration like so:
// Error: '(' expected
let _cm: <T extends Mappable> ComposeMapper<T>(f, m) => T;
But I can inline the type to declare it like this:
const _cm: <T extends Mappable<A ,B>, A, B>(f: (a: B) => B, m: T) => T = (f, m) => m.map(f);
Are generic types in type signatures expected to be right next to the parameter list, and can't be applied to another type?
Upvotes: 0
Views: 399
Reputation: 328433
Your problematic declaration looks like you're trying to create a generic variable, but TypeScript (as of v2.5) doesn't have those. TypeScript has generic types, which is what ComposeMapper<T>
is, and it has generic functions, which is what your last _cm
declaration is.
I'm confused about your typings though, anyway. Mappable
is an interface with a generic function map()
that returns any type T
that the caller of map()
specifies? How would that be implemented? And the generic variable you want seems to return the value T
, which is only a type, so that would be a syntax error anyway. And the last line, where you are calling m.map(f)
where m
is a T
and it returns a T
? How would you implement that? I am very confused and I expect that you are also. My only recommendation is for you to flesh out what you're trying to do and maybe implement it in JavaScript without types, and then see if you can use TypeScript to annotate it. Or, possibly, spend more time reading the generics section of the handbook? Not sure.
Anyway, hope that helps; good luck!
Upvotes: 1