0

Getting a typescript error when defining any type of simple react functional component with any number of props, including using FC, ComponentWithChildren etc...

Here's a minimal reproduction

type Props = {
  className?: string;
};

const Horizontal: Props = ({ className = undefined }) => (
  <div />
);

Gives me the error

Type '({ className }: { className?: undefined; }) => Element' has no properties in common with type 'Props'.ts(2559)

I don't understand this is how i always defined components. Is there something new in typescript that i don't know about?

{
  "name": "call-mvp",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.4",
    "@testing-library/react": "^13.2.0",
    "@testing-library/user-event": "^13.5.0",
    "@types/classnames": "^2.3.1",
    "@types/jest": "^27.5.1",
    "@types/node": "^16.11.36",
    "@types/react": "^18.0.9",
    "@types/react-dom": "^18.0.4",
    "@types/react-measure": "^2.0.8",
    "@types/react-router-dom": "^5.3.3",
    "@types/styled-components": "^5.1.25",
    "classnames": "^2.3.1",
    "react": "^18.1.0",
    "react-dom": "^18.1.0",
    "react-measure": "^2.5.2",
    "react-router-dom": "^6.3.0",
    "react-scripts": "5.0.1",
    "styled-components": "^5.3.5",
    "typescript": "^4.6.4",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}


2 Answers 2

1

you can change it to:

const Horizontal: FC = ({ className = undefined } : Props)
Sign up to request clarification or add additional context in comments.

Comments

1

This should work :

const Horizontal = ({ className = undefined } : Props) => ....

2 Comments

Ok weird, but what has changed compared to before? I have old code that worked fine with that definition
Perhaps the definition has changed overtime. I know i've always use this syntax so it would be hard for me to say what change in a older version

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.