S
S
SkrrGoogle2022-01-15 18:08:49
typescript
SkrrGoogle, 2022-01-15 18:08:49

Typescript type error, how to fix?

Good evening!
I have an interface:

export interface Pokemon { 
id: number 
name: string 
base_experience: number 
height: number 
is_default: boolean 
order: number 
weight: number 
abilities: object[] 
forms: object[] 
game_indices: object[] 
held_items: object[] 
location_area_encounters: string 
moves: object[] 
sprites: object 
species: object 
stats: object[] 
types: object[] 
}


Next, in the reducer, I declare an interface for the initial state:
interface PokemonState { 
pokemonData: Pokemon; 
isLoading: boolean; 
isError: string; 
}


Well, actually, I declare the state itself
const initialState: PokemonState = { 
pokemonData: {}, 
isLoading: false, 
isError: "", 
};


But as a result I get an error in the pokemonData field:
Type '{}' is missing the following properties from type 'Pokemon': id, name, base_experience, height, and 13 more


The data in pokemonData comes from the API as needed.

Please help me fix it!

PS The interesting thing is that if you specify the Pokemon[] type in the interface for pokemonData and write pokemonData: [] in the initial state in the field, then the error will disappear, and the

PSS error will occur with the OBJECT Is it the right decision to make all fields in the Pokemon type optional ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Kadyrov, 2022-01-31
@Ahmad66617

You have an answer at the end of the post - in this case, make the fields optional, or accept a partial object through Partial in your case

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question