This import works fine under babel:
import React from "react";
But under TypeScript it complains of:
(1,8): error TS1192: Module '"/Volumes/repos/tc-web-ts/node_modules/@types/react/index"' has no default export.
Have you tried:
import * as React from 'react';
Yes. But I have hundreds of files like this. I thought TypeScript was a superset of JavaScript?
Add "allowSyntheticDefaultImports": true to your tsconfig.json if you want it to work like this.
^ + IME change tsconfig.json > compilerOptions.module to 'es2015'. That's the configuration I've been using on my React/TS projects, allows for the usual default import syntax that most have become accustomed to.
Add "allowSyntheticDefaultImports": true to your tsconfig.json if you want it to work like this.
This did not work. Now I am getting this error:
Uncaught TypeError: Cannot read property 'render' of undefined
Here is the entire app:
import React from "react";
import ReactDOM from "react-dom";
function render() {
ReactDOM.render(
<h1>Hello</h1>,
document.getElementById("root")
);
}
render();
Changing to this fixes it:
import * as React from "react";
import * as ReactDOM from "react-dom";
This has to do with the CommonJS/ES module interop, which is not specified in the ECMAScript standard. In the near future, I believe that Babel and TypeScript will be more aligned on this.
The funny thing is that these are all runtime errors.
It's a bit ironic that after converting my code base to TypeScript, I'm now plagued with _runtime_ errors.
Same issue.
@bluetech
"allowSyntheticDefaultImports": trueto yourtsconfig.jsonif you want it to work like this.
This is temporary solution.
I think it's a bug when coding with JS and TS.
I just had the same error, this was the first hit on a google search. Was strange to me as I was importing above just fine and then saw the difference:
import User from "user";
had the error. This:
import {User} from "user";
did not. Might help someone else.
Try this "esModuleInterop": true
@wk-j it works.thanks
@wk-j Your solution breaks my import of moment.js.
@Mnuzz You can import moment like this
import moment from "moment"
Apparently not. I'll set up a sample project and show you what happens.
Neither "allowSyntheticDefaultImports" nor "esModuleInterop" works in my case. Tried fresh CRA with react-scripts-ts and setting up from scratch with Typescript 2.7.2.
@Mnuzz did you get around to put together the sample project?
need both
```
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
````
@gogowings Thanks it's work.
I have set
"allowSyntheticDefaultImports": true
"esModuleInterop": true
but It didn't seem to work.
mine is Angular though
@wtilton
this is because user was a constant export
@Jun711 If your module is esnext, try opting in "moduleResolution": "node" in your tsconfig. (Reference)
Try this :
import {React} from "react";
@gogoyqj Thanks. I solved this problem.
I hope my config helps other.
"compilerOptions": {
"sourceMap": true,
"module": "esnext",
"moduleResolution": "node",
"target": "es5",
"jsx": "react",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"typeRoots": [
"./node_modules/@types/"
],
}
Following the README, you are supposed to make the following changed in tslint.json:
{
- "extends": ["tslint:recommended", "tslint-react", "tslint-config-prettier"],
+ "extends": [],
+ "defaultSeverity": "warning",
"linterOptions": {
"exclude": [
"config/**/*.js",
"node_modules/**/*.ts"
]
}
}
from https://github.com/Microsoft/TypeScript-React-Starter#overriding-defaults
You can also simply add:
+ "defaultSeverity": "warning",
And the issue should go simply be a warning in the console.
Combination of
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
and in addition "noImplicitAny": false did the trick for me, but I don't really like this solution
Most helpful comment
Add
"allowSyntheticDefaultImports": trueto yourtsconfig.jsonif you want it to work like this.