Reputation: 3994
I have this interface declared:
interface XhrCaller {
(...args: (string|unknown)[]): Promise<Record<string, unknown>>
}
When I go ahead to define a get
fn like
const getCall : XhrCaller = (pathParam: string) => new Promise((resolve, reject) ...
I get an error that says
Type '(pathParam: string) => Promise<Record<string, unknown>' is not assignable to type 'XhrCaller'
Clearly, there is an issue with the way I have declared the interface.
Please help.
Upvotes: 0
Views: 103
Reputation: 2043
Since you have already declare type for XhrCaller
, you don't have to annotate pathParam
TypeScript already know its type is string|unknown
Link to TS Playground:
interface XhrCaller {
(...args: (string | unknown)[]): Promise<Record<string, unknown>>
}
const getCall : XhrCaller = (pathParam) => new Promise((resolve, reject) => {
resolve({ test: 'test' });
});
getCall('abc'); // ok
getCall(123); // ok
Beware that you used unknown
and it behaves like any
so you lost all type checking when use getCall
getCall('abc'); // ok
getCall(123); // ok
Also, you did declare union type, but that used to check for type when you apply a value like getCall(123)
not when you declare.
Your example basically declare another type (pathParam: string) => Promise<unknown>
which is not compatible with XhrCaller
const getCall : XhrCaller = (pathParam: string) => new Promise((resolve, reject) => {
resolve({ test: 'test' });
});
Upvotes: 1