Ответ 1
Тип animals.get(cageNumber)
- ?Animal
, а не Animal
. Вам нужно проверить, что это не undefined:
function feedAnimal(cageNumber:number) {
const animal = animals.get(cageNumber);
if (!animal) {
return;
}
// ...
}
Что такое подходящий способ для ecmascript-6 Map
объекты в flowtype?
const animals:Map<id, Animal> = new Map();
function feedAnimal(cageNumber:number) {
const animal:Animal = animals.get(cageNumber);
...
}
Ошибка
const animal:Animal = animals.get(cageNumber);
^^^^^^^^^^^^^^^^^^^^^^^^ call of method `get`
const animal:Animal = animals.get(cageNumber);
^^^^^^^^^^^^^^^^^^^^^^^^ undefined. This type is incompatible with
const animal:Animal = animals.get(cageNumber);
^^^^^^^ Animal
Тип animals.get(cageNumber)
- ?Animal
, а не Animal
. Вам нужно проверить, что это не undefined:
function feedAnimal(cageNumber:number) {
const animal = animals.get(cageNumber);
if (!animal) {
return;
}
// ...
}