I am working on a TypeScript/React project (just getting used to it, haven't written React in a year, etc.), and having an issue.
When I wrote this component, I followed some docs that I found, but I'm getting a TS1128 (Declaration or statement expected) error at the end of this file, and I can't figure out why:
import * as React from 'react';
import Count from './CountDisplay';
interface State {
count: number;
}
class Counter extends React.Component<{}, State> {
state: State = {count: 0};
increment() {
this.setState({
count: (this.state.count + 1)
});
}
decrement() {
this.setState({
count: (this.state.count - 1)
});
}
render(): JSX.Element {
return (
<div>
<Count count={this.state.count}/>
<button onClick={this.increment}>Increment</button>
<button onClick={this.decrement}>Decrement</button>
</div>
);
}
}
export default Counter;
I don't know, why I keep getting an error, because the code looks fine (or so I thought), but I could be wrong.
Below is my TSConfig.json, because I figured maybe it's relevant to the issue:
{
"compilerOptions": {
"outDir": "./dist",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es6",
"jsx": "react"
}
}
Closing and reopening the file fixes it as well. It seems IntelliJ TypeScript parsing can get out of sync.
I might be a little late to the party, but I also experienced this and what fixed it for me were the following steps.
Now I'm not exactly sure why that fixes the issue, but I have a few guesses. I'm using IntelliJ, which is I assume what you're using too since I've never had this issue elsewhere, and TS compiles just fine even with the issue present. It's more of an in-editor-error kind of deal (for me at least). Reopening the editor may fix this as well.
Hope this all helps!
Unchecking Recompile on changes in Settings > Language & Frameworks > Typescript > Typescript Language Service solved the issue, albeit the underlying cause remains unknown.
When I had this problem in WebStorm 2019.3.1 I was able to resolve it by going to File > Invalidate Caches / Restart... and pressing the "Invalidate and Restart" button. When WebStorm restarted the error was gone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With