0
interface Props {
  nameRu?: string,
  id?: number,
  nestedChildren?: Props[]
}

export const getData = <T extends Props>(data: T[]) => data.map(item => ({
  label: item.nameRu,
  value: item.id,
  ...((item.nestedChildren && !isEmpty(item.nestedChildren))
    ? { nestedChildren: getData(item.nestedChildren) }
    : {}),
}))

Есть такая функция, ts выдает такую ошибку.

Function implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.

Может есть у кого соображения как это испправить?

2 ответа 2

0

Ну если просто избавиться от ошибки, то можно так

export const getData = <T extends Props>(data: T[]): object => data.map(item => ({
	label: item.nameRu,
	value: item.id,
	...((item.nestedChildren && !isEmpty(item.nestedChildren))
	    ? { nestedChildren: getData(item.nestedChildren) }
	    : {}),
}))

А вообще по-хорошему надо вместо object прописать интерфейс/тип, возвращаемый данной функцией

0

Указать возвращаемый тип:

interface Props {
  nameRu?: string,
  id?: number,
  nestedChildren?: Props[]
}

type Data = {
  label?: string,
  value?: number,
  nestedChildren?: Data[]
}

export const getData = <T extends Props>(data: T[]): Data[] => data.map(item => ({
  label: item.nameRu,
  value: item.id,
  ...((item.nestedChildren && !isEmpty(item.nestedChildren))
    ? { nestedChildren: getData(item.nestedChildren) }
    : {}),
}))

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.