1

Не знаю как присвоить тип к children

const RequireAuth = ({children}) => {
  const location = useLocation();
  const auth = false; //fake auth

  if (!auth) {
    return <Navigate to='/login' state={{from: location}} />
  }
  return children
}

Вот так пробовал, не работает

const RequireAuth = ({children: object}) => ...

2 ответа 2

1
const RequireAuth = ({children}: PropsWithChildren<{}>) => {
const RequireAuth = ({children}: {children: ТутНужныйТип}) => {
1

Есть как минимум два пути

  1. указать тип параметра:

    • ({children}: {children: ChildrenType}) => {
    • ({children}: ArgType) => {, где ArgType - тип с полем children
  2. указать тип функции

    const RequireAuth: ({children:ChildrenType})=>ReturnType = ({children}) => {
    

Ввиду того, что используется реакт, можно воспользоваться встроенным типом для функциональных компонентов FC

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

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