Here is the directory structure:
root/
tsconfig.json
ts/
index.ts
index.spec.ts
I want separate tsconfig.json settings for spec files and production code. For example, I want "noImplicitAny": true
for my production code and "noImplicitAny": false
for my test code.
I also want Visual Studio Code to be in sync with tsc
, eslint
, etc. In other words, if Visual Studio Code reports an error, I want my build process to report the same error, and vice versa.
Although I’ve found solutions that get me 90% of the way, I’ve yet to find a solution that completely works. Usually, it’s VS Code reporting errors that aren’t actually there.
How do I meet all these requirements?
2
Answers
Don't use this: compiles and builds, but doesn't work (see my other answer for details)
The "trick" is to use project references. Although the linked documentation states exactly that and it's not exactly hidden, it is missing some crucial example configurations. It's also written under the assumption you want separate test and source directories, which is not the case here.
Two new tsconfigs are added to the project at the root:
Here is the root
tsconfig.json
. Visual Studio Code only readstsconfig.json
files with this name, so it has to function as a sort of base configuration:tsconfig.src.json
tsconfig.spec.json
index.ts
index.spec.ts
I realized after the fact that there were issues with my original answer. For one, the
tsconfig.spec.json
did not havecomposite
set. Technically, that’s an error, but it’s only reported if another tsconfig references it AND that other tsconfigincludes
> 0 files.Perhaps even more importantly, the
tsconfig.spec.json
should have referenced thetsconfig.src.json
. That didn’t appear as an error because the files in my previous example didn’t import across reference boundaries.I’m keeping the first answer around for prosperity. It’s a good (bad?) example demonstrating how your tsconfigs can be error-free and build, even when they’re in an invalid state. (The irony isn’t lost on me.)
Here is a correct example. I use
// @ts-ignore
to ignore compile time errors that should (and do) occur; they provetsconfig.src.json
files can’t importtsconfig.spec.json
files.Project Structure
./package.json
./src1/subdir1/foo.spec.ts
./src1/subdir1/foo.ts
./src2/subdir2/bar.spec.ts
./src2/subdir2/bar.ts
./tsconfig.json
./tsconfig.spec.json
./tsconfig.src.json