I've encountered an issue with jQuery Terminal. My d.ts file is quite large, but it's not functioning correctly. I attempted to update dependencies, leading to everything breaking. Recently, I've been unable to update TypeScript due to errors (post version 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 though the callback function has defined types.
I verified the code in TypeScript playground and it's working as expected:
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 }]);
In the function 'hey', types are obtained from the callback definition. Can anyone suggest why this may be happening and how to rectify it? The issue arises when I don't update TypeScript, resulting in errors from babel_traverse, almost as if TypeScript isn't type-checking the code at all, producing numerous errors on invalid syntax. Refer to: Angular: node_modules/@types/babel _traverse/index.d.ts(1137,43): error TS1109: Expression expected
If you're unsure of the solution, any tips on how to debug my TypeScript d.ts file to pinpoint the issue myself would be greatly appreciated.
EDIT:
Here's a snippet of the type declarations:
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 issue is that this worked fine in the previous version of TypeScript.
Additionally, here's my tsconfig.json
:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"strict": true,
"lib": ["es2015", "ES2018.Promise", "dom"]
},
"exclude": ["npm"]
}
Furthermore, I have the following code in test.ts to 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'));
}]);