Typescript styled-component error: "Type '{ children: string; }' has no properties in common with type 'IntrinsicAttributes'."
Asked Answered
M

10

70

Getting a typescript error on my styled-component

Type '{ children: string; }' has no properties in common with type 'IntrinsicAttributes'.ts(2559)

import React from 'react'

import { NotificationSuccess, NotificationError } from '../../styles'

interface IProps {
  error?: boolean;
  message: string;
}

export const Notification = (props: IProps) => {
  const Note = () => props.error ? NotificationError : NotificationSuccess;
  // Error happens on <Note>
  return (<Note>{props.message}</Note>);
}

And the styles:

import styled from 'styled-components';

export const NotificationDiv = styled.div`
  z-index: 11;
  position: fixed;
  left: 50%;
  margin-left: -160px;
  top: 1rem;
  padding: 1.5rem;
  width: 320px;
  height: auto;
  text-align: center;
  color: ${props => props.theme.offWhite};
  border-radius: 5px;
  cursor: pointer;
`

export const NotificationSuccess = styled(NotificationDiv)`
  background: ${props => props.theme.green};
`

export const NotificationError = styled(NotificationDiv)`
  background: ${props => props.theme.red};
`

enter image description here

I found this answer here, and I did upgrade my package.json to the following, but that still didn't help:

Why this wrapped styled-component errors "has no properties in common with"

"styled-components": "4.0.3",
"@types/styled-components": "4.0.3",
"babel-plugin-styled-components": "^1.10.0",

Full package.json

{
  "name": "",
  "version": "2.0.0",
  "main": "index.js",
  "scripts": {
    "dev": "next -p 7777",
    "build": "next build",
    "start": "next start -p 8000",
    "test": "NODE_ENV=test jest --watch --no-cache",
    "test-win": "SET NODE_ENV=test&& jest --watch"
  },
  "license": "ISC",
  "dependencies": {
    "@zeit/next-sass": "^1.0.1",
    "@zeit/next-typescript": "^1.1.1",
    "axios": "^0.18.0",
    "decko": "^1.2.0",
    "downshift": "^2.2.3",
    "enzyme": "^3.6.0",
    "enzyme-adapter-react-16": "^1.5.0",
    "graphql": "^14.0.2",
    "graphql-tag": "^2.9.2",
    "graphql-tools": "^4.0.0",
    "lodash.debounce": "^4.0.8",
    "next": "^7.0.2",
    "next-routes": "^1.4.2",
    "node-sass": "^4.11.0",
    "nprogress": "^0.2.0",
    "prop-types": "^15.6.2",
    "ramda": "^0.26.1",
    "react": "^16.7.0",
    "react-adopt": "^0.6.0",
    "react-dom": "^16.7.0",
    "react-redux": "^6.0.0",
    "react-transition-group": "^2.5.0",
    "redux-devtools-extension": "^2.13.8",
    "redux-thunk": "^2.3.0",
    "styled-components": "4.0.3",
    "tslint": "^5.12.1",
    "tslint-react": "^3.6.0",
    "typescript": "^3.2.4",
    "waait": "^1.0.2"
  },
  "devDependencies": {
    "@babel/plugin-proposal-decorators": "^7.3.0",
    "@babel/preset-typescript": "^7.1.0",
    "@types/enzyme": "^3.1.15",
    "@types/jest": "^23.3.13",
    "@types/next": "^7.0.6",
    "@types/ramda": "^0.25.49",
    "@types/react": "^16.7.20",
    "@types/react-dom": "^16.0.11",
    "@types/react-redux": "^7.0.1",
    "@types/styled-components": "4.0.3",
    "@types/zeit__next-typescript": "^0.1.1",
    "babel-core": "^7.0.0-bridge.0",
    "babel-jest": "^24.1.0",
    "babel-plugin-sass-vars": "^0.2.1",
    "babel-plugin-styled-components": "^1.10.0",
    "casual": "^1.5.19",
    "enzyme-to-json": "^3.3.4",
    "jest": "^24.1.0"
  },
  "jest": {
    "setupTestFrameworkScriptFile": "<rootDir>/jest.setup.js",
    "testPathIgnorePatterns": [
      "<rootDir>/.next/",
      "<rootDir>/node_modules/"
    ],
    "transform": {
      ".*": "babel-jest",
      "^.+\\.js?$": "babel-jest",
      "^.+\\.ts?$": "babel-jest",
      "^.+\\.tsx?$": "babel-jest"
    },
    "moduleFileExtensions": [
      "js",
      "json",
      "ts",
      "tsx"
    ],
    "modulePaths": [
      "<rootDir>/components/",
      "<rootDir>/pages/",
      "<rootDir>/shared/"
    ]
  }
}

Membrane answered 12/3, 2019 at 20:11 Comment(2)
Did you try deleting node_modules and reinstalling?Subsequence
Please consider defining the function component as a generic type based on the interface. e.g. const Notification: React.FC<IProps>Robertaroberto
C
22

<Note>{props.message}</Note> is same as Note({ children: props.message }) so TypeScript is complaining that function Note doesn't take any arguments and function type doesn't match. It has nothing to do with styled-components.

(IntrinsicAttributes is probably the default interface you extend when you write a functional component. Or something like that idk xD)

My best guess is you want const Note = props.error ? NotificationError : NotificationSuccess; instead of what you have written.

PS. I might be wrong but I'm mostly sure this is the case.

Casebook answered 12/3, 2019 at 20:18 Comment(1)
You were right, yeah I didn't need the = () => part.Membrane
L
62

If you create a component:

<DeliverNow>

</DeliverNow>

it automatically receives props. When you declare it:

const DeliverNow = () => {}

TypeScript will tell you that they don't match, since in reality, DeliverNow receives props.

It should actually be:

const DeliverNow = (props:any) => {}
Loblolly answered 30/9, 2019 at 11:5 Comment(1)
No, it should not be any as it's discouraged to use. I have solved it by removing React.FC and adding children prop with the type React.ReactNode.Amain
C
22

<Note>{props.message}</Note> is same as Note({ children: props.message }) so TypeScript is complaining that function Note doesn't take any arguments and function type doesn't match. It has nothing to do with styled-components.

(IntrinsicAttributes is probably the default interface you extend when you write a functional component. Or something like that idk xD)

My best guess is you want const Note = props.error ? NotificationError : NotificationSuccess; instead of what you have written.

PS. I might be wrong but I'm mostly sure this is the case.

Casebook answered 12/3, 2019 at 20:18 Comment(1)
You were right, yeah I didn't need the = () => part.Membrane
D
13

In react 18 they removed children as a default prop on the FC type. One potential fix is to do

export const Asdf = ({children}: {children: React.ReactNode}) => {
// instead of 
export const Asdf: React.FC = ({children}) => {
Durbin answered 7/9, 2023 at 9:31 Comment(0)
D
4

I got this error:

Type '{ children: string; }' has no properties in common with type 'IntrinsicAttributes'.ts

When dynamically added tags in my react application. I found a great solution that has nothing to do with typescript or styled-components.

It is enough to create a node through React.createElement

For example:

const Title: React.SFC<TitleProps> = ({ tag, styled, children }) =>
  React.createElement(tag, { ...styled }, children);

const TitleStyled = styled(Title)`Your styled`
Dispatch answered 5/3, 2020 at 9:44 Comment(0)
O
3

In react 18, They removed children as a default prop on the FC type. Please use below code to fix the issue.

Old:
export const ContextClass: React.FC = ({children}) => {}

New:
export const ContextClass = ({children}: {children: React.ReactNode}) => {}
Obligor answered 15/4, 2024 at 6:30 Comment(0)
S
2

instead of using function declaration, use function expression. EX:

export default function RootLayout({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    <html lang="en">
      <body>{children}</body>
    </html>
  )
}
Straphanger answered 22/6, 2023 at 19:16 Comment(0)
C
1

This was the fix for me. I was trying to name my own component the same name as a dependency component and VS Code automatically imported from the dependency instead of my component.

Caulescent answered 25/3, 2022 at 0:35 Comment(0)
L
1

As you can read on the Component documentation:

/**
 * A general `Component` has no implicit `children` prop.  If desired, you can
 * specify one as in `Component<{name: String, children: JSX.Element>}`.
 */
export declare type Component<P = {}> = (props: P) => JSX.Element;

So, in order to get rid of this warn, you must pass a generic that includes the children or whatever props you want. The correct implementation is:

export const Notification: Component<IProps> = (props) => {
  const Note = () => props.error ? NotificationError : NotificationSuccess;
  // Error happens on <Note>
  return (<Note>{props.message}</Note>);
}

You wont need to declare the props type, since it is intrinsic to the Component<P>.

Lynch answered 24/5, 2022 at 23:18 Comment(0)
N
0

Most common case to get that error is when you name your styled-component the same name as function component. Just give some postfix to your style components like ButtonStyled or ButtonContainer and you are g2g

Nurture answered 25/8, 2021 at 16:47 Comment(0)
J
0

I had different versions of @types/react and @types/react-dom. Correcting this and using the same version across the various packages resolved the issue.

Jinn answered 19/4, 2024 at 19:20 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.