Issue
Is it possible in TypeScript to define a function type and extend its argument list in another type (overloading function type?)?
Let's say I have this type:
type BaseFunc = (a: string) => Promise<string>
I want to define another type with one additional argument (b: number) and the same return value.
If at some point in the future BaseType adds or changes arguments this should also be reflected in my overloaded function type.
Is this possible?
Solution
UPD: it's possible to extend a function's parameters in TypeScript. See Titian's answer.
Original answer: I doubt that it can be done in the way you described. However, you can try something like this:
interface BaseOptions {
a: string;
}
type BaseFunc = (options: BaseOptions) => Promise<string>
interface DerivedOptions implements BaseOptions {
b: number;
}
type DerivedFunc = (options: DerivedOptions) => Promise<string>
Another advantage of this approach is that you have named parameters for free. So it's going to be cleaner from a caller side than just calling BaseFunc or DerivedFunc with positional arguments. Just compare:
someFuncA(1, undefined, true);
// vs
someFuncB({nofiles: 1, enableLogging: true}); // and bar: undefined is just omitted
Answered By - Ivan Velichko
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.