0

I faced the following error when I send the remaining props, (...props) to a <div> element. I tried several solutions but didn't succeed.

React.createElement

with casting?

Error:(35, 10) TS2322: Type '{ children: ReactNode; defaultChecked?: boolean | undefined; defaultValue?: string | number | readonly string[] | undefined; suppressContentEditableWarning?: boolean | undefined; ... 249 more ...; onTransitionEndCapture?: ((event: TransitionEvent<...>) => void) | undefined; }' is not assignable to type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>'.
  Type '{ children: ReactNode; defaultChecked?: boolean | undefined; defaultValue?: string | number | readonly string[] | undefined; suppressContentEditableWarning?: boolean | undefined; ... 249 more ...; onTransitionEndCapture?: ((event: TransitionEvent<...>) => void) | undefined; }' is not assignable to type 'HTMLAttributes<HTMLDivElement>'.
    Types of property ''aria-relevant'' are incompatible.
      Type '"text" | "additions" | "additions removals" | "additions text" | "all" | "removals" | "removals additions" | "removals text" | "text additions" | "text removals" | undefined' is not assignable to type '"text" | "additions" | "additions text" | "all" | "removals" | undefined'.
        Type '"additions removals"' is not assignable to type '"text" | "additions" | "additions text" | "all" | "removals" | undefined'.

The code:

import React, {FunctionComponent} from 'react';
import classNames from 'classnames';

type Direction = 'row' | 'column';

interface FlexProps extends React.HTMLAttributes<HTMLDivElement> {
    direction?: Direction;
    reverse?: boolean;
    inline?: boolean;
}

const Flex: FunctionComponent<FlexProps> = (
    {
        direction = 'row',
        reverse = false,
        inline = false,
        ...props
    }: FlexProps
): JSX.Element => {
    const flex: string = inline ? 'inline-flex' : 'flex';

    const directionReverse: string = [
      'flex',
      direction,
      reverse && 'reverse'
    ].join('-');

    const className: string = classNames(
        flex,
        directionReverse,
        props.className
    );

    return (
        <div className={className} {...props}>
            {props.children}
        </div>
    );
};

I have already tried with:

DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
4
  • Your Flex component looks good.. I just copy it and worked for me. What are you doing to get this error? Didn't get it quite well.... (I just did this <Flex>Test</Flex> ) Commented Sep 5, 2020 at 15:21
  • @LuisPauloPinto when I want to test my component I have this error or when I run typecript "tsc" Commented Sep 5, 2020 at 15:27
  • Same for me, everything works just fine. Perhaps, check the versions of the typings that you use? Commented Sep 7, 2020 at 15:49
  • I suspect, the issue is not with component definition, but rather with its usage, - somewhere out there, there's the code <Flex aria-relevant="additions removals"> Commented Sep 7, 2020 at 15:51

2 Answers 2

2
+50

TL;DR - use aria-relevant="all" instead.


There's a code in your app that might look like this

<Flex ... aria-relevant="additions removals">...</Flex>

You can find it by searching globally on your project with regex:

<Flex(.|\n)*?aria-relevant="additions removals"

According to the MDN docs:

A space-delimited list of one or more of the following values:

  • additions Element nodes added to the accessibility tree within the live region; should be considered relevant.
  • removals are deletion of nodes in the accessiblity tree; should be considered relevant.
  • text are changes to the textual content of existing nodes; should be considered relevant.
  • all is equivalent to additions removals text.
  • aria-relevant="additions text" is the default value on a live region.

In your case, you may want to use all instead of your current value (additions removals) This is equivalent to additions removals text and it's a valid, what I can't say about the value "additions removals".

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Information about aria-relevant -> github.com/DefinitelyTyped/DefinitelyTyped/pull/47012
0

Actually, I think you are not very familiar with TypeScript in ReactJS, Even you don't know about the rest of destructuring props in ReactJS, the div JSX will transform to read <div> HTML element by ReactDOM, so you should care about every passing prop because every prop will transform to HTML Attribute, so passing rest to is a fault, I mean:

~~~ ({
 somePropName,
 ...rest
}) => (
  <div {...rest} // -- this passing rest is a fault

And about TypeScript in ReactJS, I really offer you to buy Maximilian Schwarzmüller, TypeScript training, and especially study the ReactJS module.

By the way, the right way of writing of your component is the below code:

import React, { FC, ReactNode } from 'react';
import cn from 'classnames';

interface FlexProps {
  direction?: 'row' | 'column';
  reverse?: boolean;
  inline?: boolean;
  children: ReactNode;
  className: string;
}

const Flex: FC<FlexProps> = ({
  direction = 'row',
  reverse,
  inline,
  className,
  children,
}) => {
  const flex: string = inline ? 'inline-flex' : 'flex';

  const directionReverse: string = [
    'flex',
    direction,
    reverse && 'reverse',
  ].join('-');

  const classNames: string = cn(flex, directionReverse, className);

  return <div className={classNames}>{children}</div>;
};

export default Flex;

Why you pass a default false value to the boolean prop?? when a developer(even you) use your Flex component and do not pass a boolean prop, so it will be undefined and by the JavaScript, the coercion will happen and the value will be a _false` boolean value.

Instead of type JSX.Element pass a regular TypeScirpt-React valid type, hence, ReactNode, easy.

Also by using FC<FlexProps> there is no need to have }: FlexProps) => { only the }) => { is enough.

If in future a new prop name will be needed, Ok, add it to the interface and use it in the component but do not pass a rest of props. it is not a valid way in ReactJS development.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.