问题
I have this issue with jQuery Terminal. I have quite big d.ts file but it don't work properly I've tried to update dependencies and everything broke. From some time I was not able to update TypeScript because got this type of errors (version after 3.1)
./node_modules/.bin/tsc --noEmit --project tsconfig.json
test.ts:18:31 - error TS7006: Parameter 'command' implicitly has an 'any' type.
18 $('.term').terminal([function(command, term) {
even that callback function have types defined.
I've checked TypeScript playground and this works fine:
type arg = ((x: number) => number) | number;
function hey(list: arg[]) {
list.forEach(x => {
if (typeof x === 'function') {
x(10);
}
});
}
hey([10, function(x) { x.toFixed(10); return x }]);
I have function without types when I use hey and types is taked from my callback definition. Anybody have a clue why this may happen and how to fix it? The problem is that when I don't update typescript I've got error from babel_traverse like TypeScript don't typecheck the code at all got thousands of errors from typescript on invalid syntax see: Angular: node_modules/@types/babel _traverse/index.d.ts(1137,43): error TS1109: Expression expected
IF you don't know the answer maybe you know how to debug my typescript d.ts file so I can find the problem myself.
EDIT:
this is part of the types declaration:
type TypeOrArray<T> = T | T[];
declare namespace JQueryTerminal {
type interpreterFunction = (this: JQueryTerminal, command: string, term: JQueryTerminal) => any;
type terminalObjectFunction = (...args: (string | number | RegExp)[]) => (void | TypeOrPromise<echoValue>);
type Interpreter = string | interpreterFunction | ObjectInterpreter;
type ObjectInterpreter = {
[key: string]: ObjectInterpreter | terminalObjectFunction;
}
}
interface JQuery<TElement = HTMLElement> {
terminal(interpreter?: TypeOrArray<JQueryTerminal.Interpreter>, options?: TerminalOptions): JQueryTerminal;
}
The problem is that this worked fine in previous version of TypeScript.
and here is my tsconfig.json
:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"strict": true,
"lib": ["es2015", "ES2018.Promise", "dom"]
},
"exclude": ["npm"]
}
and I have this in test.ts that check the types in CI:
/// <reference path="./js/jquery.terminal.d.ts" />
import "jquery";
import "jquery.terminal";
function test_type<T>(x: T) {};
// this works
$('.term').terminal(function(command, term) {
term.echo(command);
});
// this also works
$('.term').terminal(function(command) {
this.echo(command);
});
// this doesn't work, function in array
$('.term').terminal([function(command, term) {
term.echo(command);
return Promise.resolve(document.createElement('div'));
}]);
回答1:
Replacing TypeOrArray
with explicit function overloads seems to fix this issue:
interface JQuery<TElement = HTMLElement> {
terminal(interpreter?: JQueryTerminal.Interpreter): any;
terminal(interpreter?: JQueryTerminal.Interpreter[]): any;
}
TS playground
来源:https://stackoverflow.com/questions/65405317/typescript-after-3-1-dont-accept-callback-functions-argument-types