Typescript configuration - how to handle shared types and tests, without building them in the output?

Viewed 16

I have my project structured like this:

/project
    ...someFolders
    /sharedTypes
    /backend
        tsconfig.json
        /src
            index.ts
            ...others
        /dist
        /tests
        ...others

I want to have access to the sharedTypes from my app, and also to not include the tests folder in my output. After a lot of searching, this is how my tsconfig looks like:

{
  "ts-node": {  "files": true },
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "rootDirs": ["src", "../sharedTypes"],
    "baseUrl": "src",
    "paths": {
      "@sharedTypes*": ["../../sharedTypes*"],
      "@/*": ["./*"]
    },
    "outDir": "dist",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
  },
  "exclude": ["tests"]
}

Now the tests folder is not included in my output, and I can access types from the sharedTypes folder. But, now I have new problems. First, I can't import using the typescript paths (that are configured in the tsconfig) in my tests files (because they are excluded). Also, after build the output in the dist looks like this:

/dist
    /backend
        /src
            index.js
            ...others
    /sharedTypes

That's even tho the sharedTypes have only types, so it doesn't really do anything in my app after build. I want typescript to build the src folder to the dist folder, so I would be able to run node dist/index.js and it will run the app.

What should I do so my output will only include the src folder files, but also allow importing types from the sharedTypes, and allow importing with the typescript paths in my tests?

0 Answers
Related