Skip to content

error: is not assignable to parameter of type 'Attrs<FastOmit<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, never>> #4088

Closed

Description

@greenpau

The previous usage of the styled components follows. Everything worked, until the recent upgrade to 6.0.4.

const StyledImageCarousel = styled.div.attrs((props: ImageCarouselProps) => props)`
  ${(props) =>
    props.is_borderless === true
      ? `// nothing`
      : `
      border-bottom: 0.15em solid ${csui.colorBackgroundButtonPrimaryActive};
      border-top: 0.15em solid ${csui.colorBackgroundButtonPrimaryActive};
      `}
  text-align: center;
`;

The error follows.

 FAIL  src/App.test.tsx
  ● Test suite failed to run

    src/components/builtin/common/ImageCarousel/content.tsx:27:46 - error TS2345: Argument of type '(props: ImageCarouselProps) => ImageCarouselProps' is not assignable to parameter of type 'Attrs<FastOmit<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, never>>'.
      Type '(props: ImageCarouselProps) => ImageCarouselProps' is not assignable to type '(props: ExecutionContext & FastOmit<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, never>) => ExecutionProps & Partial<...>'.
        Types of parameters 'props' and 'props' are incompatible.
          Type 'ExecutionContext & FastOmit<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, never>' is missing the following properties from type 'ImageCarouselProps': name, mode, is_borderless

    27 const StyledImageCarousel = styled.div.attrs((props: ImageCarouselProps) => props)`
                                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    src/components/builtin/common/ImageCarousel/content.tsx:29:11 - error TS2339: Property 'is_borderless' does not exist on type 'ExecutionContext & FastOmit<FastOmit<Substitute<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, Omit<...> & { ...; }>, never>, never>'.

    29     props.is_borderless === true
                 ~~~~~~~~~~~~~

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        5.683 s
Ran all test suites.

Activity

  1. ziolekjj commented on Jul 14, 2023

    @ziolekjj
    Contributor

    you could fix this err by passing props this way:

    const StyledImageCarousel = styled.div.attrs<ImageCarouselProps>(props => props)`
      ${(props) =>
        props.is_borderless === true
          ? `// nothing`
          : `
          border-bottom: 0.15em solid ${csui.colorBackgroundButtonPrimaryActive};
          border-top: 0.15em solid ${csui.colorBackgroundButtonPrimaryActive};
          `}
      text-align: center;
    `;
  2. greenpau commented on Jul 14, 2023

    @greenpau
    Author

    @ziolekjj , thank you for the pointer.

    Here is what I get in the browser logs. Not sure how to address it.

    styled-components: it looks like an unknown prop "urls" is being sent through to the DOM, which will likely trigger a React console error. If you would like automatic filtering of unknown props, you can opt-into that behavior via `<StyleSheetManager shouldForwardProp={...}>` (connect an API like `@emotion/is-prop-valid`) or consider using transient props (`$` prefix for automatic filtering.)
    

    image

  3. jeffgore00 commented on Jul 30, 2023

    @jeffgore00

    Hey @greenpau , a fellow consumer of the library like you but in the absence of a reply from a maintainer I can give my two cents / solution.

    The issue seems to stem from the newly-type-conscious library's effort to only pass "known HTML attributes" to underlying components. So because is_borderless is not a standard attribute for HTML elements or a div in particular, the TS error shows.

    You can use transient props (just a dollar sign $ before the prop name) to signify that you don't intend on using this prop as an actual HTML attribute.

    I tested out the below and didn't see any TypeScript errors on my end.

    export const StyledImageCarousel = styled.div<ImageCarouselProps>`
      ${(props) =>
        !props.$is_borderless &&
        `border-bottom: 0.15em solid ${csui.colorBackgroundButtonPrimaryActive};
         border-top: 0.15em solid ${csui.colorBackgroundButtonPrimaryActive}`};
      text-align: center;
    `;
  4. greenpau commented on Jul 31, 2023

    @greenpau
    Author

    @jeffgore00 , thank you! This is super helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions