TS Почему два одинаковых объекта обращаются друг к другу - PullRequest
0 голосов
/ 07 августа 2020

При изучении TS я столкнулся со следующими проблемами: Что мне делать, чтобы программа работала правильно?


type A = {
  a:number
}

type B = {
  [K in keyof A]: 1;
}

let a: A = { a: 123 };
let b: B = { a: 1 };

// give me a error
/**
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'A'.
  No index signature with a parameter of type 'string' was found on type 'A'.
*/
for (let k in b) { 
  let c = a[k];
}

// give me a error
/**
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'B'.
  No index signature with a parameter of type 'string' was found on type 'B'.
*/
for (let k in a) {
  let c = b[k];
}


Почему два одинаковых объекта обращаются друг к другу?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...