Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript - "Cannot find name" errors in React components

I am migrating my existing React code over to TypeScript and I am hitting a lot of issues, one of them being a lot of "Cannot find name" errors when I make my .js files .ts files.

Here is the code in question:

import React from 'react';  const Footer = ({ children, inModal }) => (     <footer className={'tableBottomPanel' + (inModal ? " in-modal" : "") }>         <div>             {children}         </div>     </footer> );  export default Footer; 

The five lines from <footer> to </footer> are underlined in red and give me various errors, depending where I hover my mouse, such as:

  • Cannot find name 'footer'.
  • '>' expected
  • Cannot find name 'div'
  • Unterminated regular expression literal
  • Operator '<' cannot be applied to types 'boolean' and 'RegExp'

Here is my tsconfig.json file:

{     "compilerOptions": {         "outDir": "./dist/", // path to output directory         "sourceMap": true, // allow sourcemap support         "strictNullChecks": true, // enable strict null checks as a best practice         "module": "es6", // specify module code generation         "jsx": "react", // use typescript to transpile jsx to js         "target": "es5", // specify ECMAScript target version         "allowJs": true, // allow a partial TypeScript and JavaScript codebase         "moduleResolution": "node",         "allowSyntheticDefaultImports": true,         "lib": [             "es6",             "dom"         ],         "types": [             "node"         ]     },     "include": [         "./src/"     ] } 

I am incredibly confused and would greatly appreciate some help!

like image 844
noblerare Avatar asked Jul 03 '18 15:07

noblerare


People also ask

What is the difference between TSX and TS?

tsx extension is used when we want to embed JSX elements inside the files while . ts is used for plain Typescript files and do not support adding JSX Elements. Save this answer.

What is intrinsic attribute typescript?

IntrinsicAttributes interface can be used to specify extra properties used by the JSX framework which are not generally used by the components' props or arguments - for instance key in React. Specializing further, the generic JSX.

What has no properties in common with type IntrinsicAttributes?

The React. js error "Type {children: Element} has no properties in common with type IntrinsicAttributes" occurs when we try to pass a children prop to a component that doesn't take any props. To solve the error define and type the props on the component.


1 Answers

Typescript isn't expecting to see JSX in your Typescript file. The easiest way to resolve this is to rename your file from .ts to .tsx.

JSX in Typescript Documentation

like image 155
coreyward Avatar answered Oct 04 '22 13:10

coreyward