问题
tl;dr at the bottom
I've got a webserver written in TypeScript and a client SPA written in TypeScript. My idea was to share interfaces for request and response data to take full advantage of "type-safety". My big problem is that client and server are sibling folders each with its own package.json, tsconfig.json, src-dir etc. What I want to have is something like this:
server/
├── src/
├── build/
├── tsconfig.json
└── package.json
client/
├── src/
├── build/
├── tsconfig.json
└── package.json
shared/
├── Requests.ts
└── Responses.ts
I want to be able to import the shared interfaces like this:
import { ILoginRequest } from '@shared/Requests';
I already configured my tsconfig.json
but tsc
still outputs
error TS2307: Cannot find module '@shared/Requests'.
:
Client (React) Config as an example:
{
"compilerOptions": {
"outDir": "build/dist",
"module": "esnext",
"target": "es5",
"lib": ["es6", "dom"],
"sourceMap": true,
"allowJs": true,
"jsx": "react",
"moduleResolution": "node",
"rootDirs": [
"src",
"../shared"
],
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true,
"baseUrl": ".",
"paths": {
"@shared/*": [
"../shared/*"
],
"*": [
"node_modules/*",
"src/types/*"
]
}
},
"exclude": [
"node_modules",
"build",
"scripts",
"acceptance-tests",
"webpack",
"jest",
"src/setupTests.ts"
]
}
What am I doing wrong? Is my idea even possible? Thanks in advance!
tl;dr
I want to share interfaces between two TS projects ("server" and "client") via a sibling folder ("shared") but can't figure out how to configure tsc
to make something like import { ILoginRequest } from '@shared/Requests';
possible.
来源:https://stackoverflow.com/questions/47521714/share-interfaces-between-separat-typescript-projects