jQueryターミナルでこの問題が発生しました。かなり大きなd.tsファイルがありますが、正しく機能しません。依存関係を更新しようとしましたが、すべてが壊れました。このタイプのエラーが発生したため、しばらくの間TypeScriptを更新できませんでした(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) {
そのコールバック関数でさえ、タイプが定義されています。
TypeScriptプレイグラウンドを確認しましたが、これは正常に機能します。
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 }]);
ちょっと使用すると、型のない関数があり、型はコールバック定義から取得されます。なぜこれが起こるのか、そしてそれを修正する方法の手がかりを誰かが持っていますか?問題は、typescriptを更新しないと、TypeScriptのようにbabel_traverseからエラーが発生することです。コードをタイプチェックしないと、無効な構文でtypescriptから何千ものエラーが発生します。Angular:node_modules / @ types / babel _traverse / index .d.ts(1137,43):エラーTS1109:式が必要です
答えがわからない場合は、typescript d.tsファイルをデバッグして、自分で問題を見つける方法を知っているかもしれません。
編集:
これは型宣言の一部です:
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;
}
問題は、これが以前のバージョンのTypeScriptで正常に機能したことです。
そしてここに私のtsconfig.json
:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"strict": true,
"lib": ["es2015", "ES2018.Promise", "dom"]
},
"exclude": ["npm"]
}
そして私はCIのタイプをチェックするtest.tsにこれを持っています:
/// <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'));
}]);
TypeOrArray
明示的な関数のオーバーロードに置き換えると、この問題が修正されるようです。
interface JQuery<TElement = HTMLElement> {
terminal(interpreter?: JQueryTerminal.Interpreter): any;
terminal(interpreter?: JQueryTerminal.Interpreter[]): any;
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加