0
関数型インターフェイスを作成し、それを別の関数(つまりコールバックを期待する関数)が期待する型として使用する場合、コールバック関数のパラメータはaクラス。私はそれがクラスのインスタンスの配列を期待していないときとBの2番目の呼び出しは、型チェックに失敗しなければならないと考えている。この場合関数のインターフェイスのクラスの配列の型チェック
"use strict";
class A {
/* no-op */
}
interface C {
(s: Array<A>): void
}
const B = (c: C) => {
c(["Hello World!"]);
};
B((s: Array<A>) => {console.log("Should work", s)});
B((s: A) => {console.log("Should not work", s)});
、代わりに原始的な:型チェックは、それを処理することができていないようです私は答えを検索しようとすると、私は活字体2を使用していたときに、このについての何かを見つけることができませんでした
test.ts(12,3): error TS2345: Argument of type '(s: string) => void' is not assignable to parameter of type 'C'.
Types of parameters 's' and 's' are incompatible.
Type 'string[]' is not assignable to type 'string'.
:
"use strict";
interface C {
(s: Array<string>): void
}
const B = (c: C) => {
c(["Hello World!"]);
};
B((s: Array<string>) => {console.log("Should work", s)});
B((s: string) => {console.log("Should not work", s)});
と型チェックを失敗:文字列として。 3.4。
は、ドキュメントへの迅速な答えや参考のためにあなたにNitzanをありがとう! –