Reputation: 58
When I am trying to define an overload my function with a Partial interface overloading, typescript is picking the wrong overload
interface Args {
_id: string;
name: string;
}
interface Result {
_id: string;
name: string;
}
function myFunction(arg: Args): Result;
function myFunction(arg: Partial<Args>): Partial<Result> {
return arg;
}
// Argument of type '{ _id: string; }' is not assignable to parameter of type 'Args'.
// Property 'name' is missing in type '{ _id: string; }' but required in type 'Args'
myFunction({ _id: 'aa' });
Trying to use myFunction({ _id: 'aa' } as Partial<Args>);
doesn't help
How could I make this work ?
Upvotes: 2
Views: 156
Reputation: 249606
The last signature is the implementation signature and is not part of the publicly accessible signatures. So your function really on has one accessible signature function myFunction(arg: Args): Result;
hence the error.
function myFunction(arg: Args): Result;
function myFunction(arg: Partial<Args>): Partial<Result>
function myFunction(arg: Partial<Args>): Partial<Result> {
return arg;
}
myFunction({ _id: 'aa' });// ok now
Upvotes: 2