Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot access props passed in object as function parameter TypeScript

I have interfaces like this

interface CatProps {
    feedingTime: number,
    color: string,
    old: boolean
}

interface DogProps {
    height: number,
    breed: string,
    allergies: boolean
}

interface Cat {
    name: string,
    data: CatProps[]
}

interface Dog {
    name: string,
    data: DogProps[]
}

type AnimalState = {
    dog?: Dog[],
    cat?: Cat[]
}

export interface AnimalStateEntries {
    allStatesArrays: Dog[] | Cat[]
    allStatesSingular: DogProps | CatProps
}

}

I then map through an object (ANIMALS) of type AnimalState like this, calling functionOne() and functionTwo()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const ANIMALS: AnimalState = {...}

(Object.keys(ANIMALS) as Array<keyof AnimalState >).map((identifier, index) => {
    let animal = ANIMALS[identifier]
    if(animal) {
          let {name, data} = animal
          functionOne(name, data, identifier)  
    }
})


const functionOne = (name: string, data: AnimalStateEntries['allStatesArray'], identifier: keyof AnimalState) => {
    return data.map(information => functionTwo(information, identifier)

})

const functionTwo = (data: AnimalStateEntries ['allStatesSingular'], identifier: keyof AnimalState) => {
    if(identifier === 'dog') {
         const {height, breed, allergies} = data
    } else {
         const {feeding, color, old} = data
    }
}  

But, when I try to access const {height, breed, allergies} = data and const {feeding, color, old} = data in functionTwo() I am getting

Property ‘…’ does not exist on type ‘CatProps | DogProps   Property ‘…’ does not exist on type ‘DogProps’.

Where ... are the props I am trying to access.
If I change const functionTwo = (data: any, identifier: keyof AnimalState) => {...}

It works. Why is that?

>Solution :

if(identifier === 'dog') {

gives Typescript no idea about the type of data. You will have to use some type narrowing which typescript understands.

Try to use a type predicate:

function isDog(identifier: 'dog' | 'cat', data : AnimalStateEntries ['allStatesSingular']): data is DogProps {
  if(identifier === 'dog')
  return true;
  return false;
}

Change your if condition like:

if(isDog(identifier,data)) {
         const {height, breed, allergies} = data
    }

TS Link

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading