Skip to content

Typescript: styleName usage with @types/react@^17 #292

Open

Description

@eugeneoshepkov

There's a problem with extending Attributes and HTMLAttributes interface with styleName with @types/react@^17. It simply has no effect:

Property 'styleName' does not exist on type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>'.

"@types/react-css-modules": "^4.6.2",

image

Activity

  1. yinsang commented on Jul 22, 2021

    @yinsang

    add file to your project, named react-css-module.d.ts.

    declare namespace React {
      interface Attributes {
        styleName?: string | undefined;
      }
      interface HTMLAttributes<T> {
        styleName?: string | undefined;
      }
      interface SVGAttributes<T> {
        styleName?: string | undefined;
      }
    }
  2. Anatoool commented on Aug 26, 2021

    @Anatoool

    I have the same issue with "@types/react-css-modules": "^4.6.4",

  3. max-mykhailenko commented on Feb 21, 2023

    @max-mykhailenko

    @yinsang your solution doesn't work with
    typescript 4.3.5 and react 17.0.2
    Got Property 'styleName' does not exist on type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>'

    Maybe you have some thoughts how to fix that

  4. leevben commented on Apr 29, 2024

    @leevben

    This works for me(React@18.x)
    1.

    // global.d.ts
    declare namespace JSX {
      interface IntrinsicAttributes {
        styleName?: string;
      }
    
    }
    declare namespace React {
      interface Attributes {
        styleName?: string;
      }
      interface HTMLAttributes<T> {
        styleName?: string;
      }
      interface SVGAttributes<T> {
        styleName?: string;
      }
    }
    1. turn off this eslint rule
        "react/no-unknown-property": "off"
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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions