I have been working on a React + Express project lately which has a project file structure like this:
Repo
|--ExpressApp
|--src/..
|--Common
|--types.ts
|--ReactApp
|--src/..
So the express app lives within the ExpressApp folder, the react app lives within the ReactApp folder, and inside of Common are a set of Typescript types that are shared between the express and react applications.
The react application is a create-react-app project, and I have read issues associated with importing files that reside outside of the react apps src directory. However, in the React application, I seem to be able to import the types that are specified from the Common/types.ts into the React application without any problem.
/** Common/types.ts */
// This imports ok to React app even though its outside of the src directory
export type Person {
id: string,
name: string,
role: string
};
// This does not import into React app, as it is outside of the source directory
export const smapleFunction = () => {
...
}
/** ReactpApp/src/app.ts */
// This is accepted by React
import { Person } from '../../common/types';
// This is not accepted by React
import { sampleFunction } from '../../common/types';
If I was to export a function from the Common/types.ts file and use it within the React application, then react throws the expected error that files must reside within the src directory - however, no issue is found when importing types, interfaces etc.
Could somebody please explain why this seems to be work for typescript types residing outside of src, but not for functions or other modules? I'm assuming it has something to do with how Typescript is handled within a CRA project, but I don't quite understand the process?
src/and still be imported/compiled. Path mapping supports it. Practical example: Crisp React. I'm the author./distfolder, mimics the folder structure of the monorepo instead of the the root project. If that makes sense, any ideas?