Share interfaces between separat TypeScript projects

倾然丶 夕夏残阳落幕 提交于 2019-12-10 19:28:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!