Source:  Twitter logo

It's as simple as stated above: React 16.9.22 with TypeScript 3.8.2. It doesn't affect anything, just crowds my warning window form actually useful things.

In my tsconfig I have:

{
  "compilerOptions": {    
    "esModuleInterop": true
  }
}

But every file have the line import React from 'react';

I get the linting error:

.../node_modules/@types/react/index"' can only be default-imported using the 'esModuleInterop' flag

possibly relevant deps:

  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^2.22.0",
    "@typescript-eslint/parser": "^2.22.0",
    "cross-env": "^5.2.0",
    "eslint": "^6.6.0",
    "eslint-config-react-app": "^4.0.1",
    "eslint-plugin-flowtype": "^2.0.0",
    "eslint-plugin-import": "^2.14.0",
    "eslint-plugin-jsx-a11y": "^6.2.1",
    "eslint-plugin-react": "^7.18.3",
    "typescript": "^3.8.2"
  },

What worked for me was adding the following in my tsconfig.json file in the compilerOptions:

"allowSyntheticDefaultImports": true,
"esModuleInterop": true
10 users liked answer #0dislike answer #010
George profile pic
George
import * as React from "react";
import * as ReactDOM from "react-dom";

This is the most futureproof way to import React. If you set --allowSyntheticDefaultImports (or add "allowSyntheticDefaultImports": true) in your tsconfig.json you can use more familiar imports:

import React from "react";
import ReactDOM from "react-dom";

source: https://github.com/typescript-cheatsheets/react

2 users liked answer #1dislike answer #12
HiLuLiT profile pic
HiLuLiT

Alternatively, we can separate the imports as below for example

import React, { useState, useEffect, FC } from 'react';

to

import * as React from 'react';
import { useState, useEffect, FC } from 'react';
0 users liked answer #2dislike answer #20
Bhuwan Maharjan profile pic
Bhuwan Maharjan

Copyright © 2022 QueryThreads

All content on Query Threads is licensed under the Creative Commons Attribution-ShareAlike 3.0 license (CC BY-SA 3.0).