Chinnu
Chinnu

Reputation: 304

Function implicitly has return type 'any' error

I'm calling the function but it throwing error in object : any(Function implicitly has return type 'any' error because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.ts(7024))

let getValues = (object : any) => {
  return object && typeof object === "object"
    ? [
      ...("title" in object ? [object.title] : []),
      ...Object.values(object).flatMap(getValues)
    ]
  : [];
 };


getValues(courseProperties.parent.children[0]).map((title : any) => (
  <div>{title}</div>
))

Upvotes: 3

Views: 6865

Answers (1)

Frank Fajardo
Frank Fajardo

Reputation: 7359

You should give your getValues a return type, like so:

let getValues = (object : any): any[] => {
    return object && typeof object === "object"
        ?
            [
                ...("title" in object ? [object.title] : []),
                ...Object.values(object).flatMap(getValues)
            ]
        : [];
};

but preferablly:

let getValues = (object : any): unknown[] => {
    return object && typeof object === "object"
        ?
            [
                ...("title" in object ? [object.title] : []),
                ...Object.values(object).flatMap(getValues)
            ]
        : [];
};

Upvotes: 4

Related Questions