5

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.

1
  • It should work. Probably some error in the config. I would advise you to try something simple first (import x from '../shared/xxx') and make sure it works before working on the baseUrl and paths. Commented Nov 28, 2017 at 3:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.