を使用して機能し、そのコードを使用して、エラーを生成します。活字体:異なった結果、このコードを有するtypeof演算子
interface something {
[id: string]: string | something;
}
let obj: something = {
'1': 'str',
'2': {
'3': 'str'
}
};
function isObject(obj: any): boolean {
return typeof obj === 'object' && obj !== null;
}
for (let key in obj) {
const specific = obj[key];
if (isObject(specific)) {
for (let id in specific) {
// ^^^// The right-hand side of a
// 'for...in' statement must be of type 'any',
// an object type or a type parameter
}
}
}
機能コードの代わりに、それisObject(specific)
を使用している場合しかし、それがうまく働いています:
if (typeof specific === 'object' && specific !== null) {
// ...
なぜですか?コード内の関数を削除する必要がありますか?あなたは機能コード付きif
ステートメントを使用する場合
'isObject'は*あなたが*その何かがnull以外のオブジェクトで言うかもしれないが、*それは*コンパイラに指示しません。 – jonrsharpe