Here's a possibly very simple question for the ones that have read the TypeScript handbook:
Consider the following interface: (it's from the simple-git
library which I'm trying to use).
export interface SimpleGitTaskCallback<T = string, E extends GitError = GitError> {
(err: null, data: T): void;
(err: E): void;
}
I have a function which has the following signature:
checkIsRepo(action?: types.CheckRepoActions, callback?: types.SimpleGitTaskCallback<boolean>): Response<boolean>;
The second argument presents a problem for me.
If I call the function like this (just like the first signature in the interface requires me to):
git.checkIsRepo(CheckRepoActions.IS_REPO_ROOT, (error: null, isRepo: boolean): void => {
console.log(error);
console.log(isRepo);
});
I get:
Argument of type '(error: null, isRepo: boolean) => void' is not assignable to parameter of type 'SimpleGitTaskCallback<boolean, GitError>'.
Passing a function that matches the second signature in the interface does work:
git.checkIsRepo(CheckRepoActions.IS_REPO_ROOT, (isRepo: any): void => {
console.log(isRepo);
});
It works, but it prints null because what I'm getting in isRepo is actually the null error (since the command completes successfully.
But what I want is to understand how to pass a function satisfying the first signature.
Worth noting:
Doing this with promises works just fine:
git
.checkIsRepo(CheckRepoActions.IS_REPO_ROOT)
.then((isRepo: boolean): void => {
console.log(isRepo);
})
.catch((err: GitError): void => {
console.log(err);
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…