I'm currently facing an issue in my ReactJS project using Webpack2 and TypeScript. Everything is functioning perfectly except for one thing - I've been struggling to move my self-written interfaces into separate files so they are accessible throughout the application.
Initially, I had all interfaces defined in files where they were being used, but as the project grew, I needed some interfaces to be shared among multiple classes, leading to complications. Despite modifying my tsconfig.json
and relocating the files, both my IDE and Webpack continue to throw errors like "Could not find name 'IMyInterface'."
Here is a snippet from my current tsconfig.json
:
{
"compilerOptions": {
// Compiler options here
},
// Exclude paths here
}
The tsconfig.json
file resides in the root directory, source code in the ./src
folder, and custom .d.ts
files under ./typings
, included in the typeRoots
.
I have tested this setup with TypeScript versions 2.1.6 and 2.2.0 without success.
A possible solution involves moving the typings
directory into src
and then importing interfaces like so:
import {IMyInterface} from 'typings/blah'
. However, this approach does not feel right to me as it's unnecessary complexity. I want these interfaces to be seamlessly available across the entire application.
For example, a sample interface app.d.ts
looks like:
interface IAppStateProps {}
interface IAppDispatchProps {}
interface IAppProps extends IAppStateProps, IAppDispatchProps {}
Do I need to export them or declare? Do I have to enclose them in a namespace?
Update (October 2020)
Given the ongoing popularity of this question, let me elaborate on the solution further.