0

Создаю компонент Text в него вложен другой компонент Tag. Как я могу его типизировать, что бы заработало? Сейчас появляется ошибка и он не принимает нужные стили. Ошибка такая:

Type '{ children: ReactNode; classNames: any; }' is not assignable to type 'IntrinsicAttributes'. Property 'children' does not exist on type 'IntrinsicAttributes'.ts(2322)

Код:

import React, { ReactNode } from 'react';
import styles from './Text.module.css';
import classNames from 'classnames/bind';

const cx = classNames.bind(styles);

interface ITextProps {
    variant: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'p1' | 'p2' | 'p3';
    bold?: boolean;
}

const tags = {
    h1: 'h1',
    h2: 'h2',
    h3: 'h3',
    h4: 'h4',
    h5: 'h5',
    h6: 'h6',
    p1: 'p',
    p2: 'p',
    p3: 'p',
};

const Text: React.FC<ITextProps> = ({ children, variant, bold }) => {
    const Tag = tags[variant];

    return <Tag classNames={cx({ title: variant === 'h1' })}>{children}</Tag>;
};

export default Text;
4
  • по идее в Tag должна быть функция/компонент, а не строка
    – Grundy
    14 апр 2020 в 9:11
  • тоесть возвращаться должна функция со своими пропсами? не очень понятно всё равно 14 апр 2020 в 10:39
  • нет, чтобы написать <Tag - в Tag должна быть функция
    – Grundy
    14 апр 2020 в 16:26
  • Choosing the Type at Runtime
    – Grundy
    14 апр 2020 в 16:35

1 ответ 1

0
React.FC<ITextProps> // означает что props строго соответствуют ITextProps

а на самом деле: в ITextProps отсутствует children, о чем и сообщает компилятор
(добавьте children в ITextProps)

Ваш ответ

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

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