In my Typescript 2.2.1 project in Visual Studio 2015 Update 3, I am getting hundreds of errors in the error list like:
Cannot write file \'C:/{{my-project
It seems like this issue was fixed for me by updating to Typescript 2.3.x
Also, using Visual Studio 2017 was a big improvement as well. I highly recommend that you make both of these updates though.
There's several possible causes to this.
outDir
to "dist" or the name of another same-level folder. (prefixing with './' is unnecessary). This is where the build files go.allowJs
to false or delete the line. Note: enabled, allowJs will conflict with the declaration setting/flag. It isn't enabled by default.exclude
.main
to "index" or some other chosen name. Don't prefix with the build folder (eg "dist/index"), nor the unnecessary "./".types
(modern alias of typings
) to "index". Adding the extensions (.d.ts or .js) is unnecessary.Though you can have it a million different ways, for the sake of simplicity and developing understanding it's best to stick to common practices at first - like using "dist", simple tsconfig.json and package.json files at the same level in the tree, and so on. Of course, rooting through the files of your node_modules would also deepen your grasp, but there are more rewarding things in life.
I have run into this issue due to VSCode autocompleting a file in the dist/
folder.
import { SomeClass } from '../../dist/xxx/someclass'
To solve the problem just fix the import:
import { SomeClass } from './someclass'
I had the same issue and it's because of the exclude
option. If you specify exclude
option, you have to add the output directory also.
I had the exclude option like this "exclude": ["resources/js/**/*", "node_modules"]
and the output directory set as outDir:"./build"
.
Adding build
to the exclude option fixed the issue.
exclude:["resources/js/**/*", "node_modules", "build"]
Because the problem can have a wide set of reasons! I'm going to share my experience on when i encountered the error !
In my case! I had two files ! One src/index.ts
the other on src/test/logToFile.directTest.ts
.
excluding src/test/logToFile.directTest.ts
solved the problem!
It seems each resolution was trying to write to the same file!
My config for the declaration was:
{
"compilerOptions": {
"declaration": true,
"declarationDir": "./dist",
"module": "commonjs",
"noImplicitAny": true,
"lib": ["ESNext", "DOM"],
"outDir": "./dist",
"target": "es6",
"moduleResolution": "node",
"resolveJsonModule": true,
"esModuleInterop": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}
And everything was setup correctly! You can notice that i excluded the dist
directory. And correctly set the outDir
which are necessary (you can get the error if you don't! And all the other answer mentioned that).
More then that i was using the config with other repositories and packages! And i haven't any problems!
At the end ! it was this:
import { Logger } from '../..';
My import was wrong!
It should have been:
import { Logger } from '..';
Porting the module to it's own repository i forget to change the import!
And i included back the file! And tested and all worked well!
And to bring the benefit from the story!
An import can refer to root (for example) and automatically redirect back to dist! And so the .d.ts file! Typescript should normally throw an error! As that stand out of the directory folder! But it didn't! And made that error!
And to bring more benefit
In short it's described here:
https://github.com/microsoft/TypeScript/issues/6046#issuecomment-210186647
in short an input file is either a file that you pass on the command line, or that is a dependent of one of the files you pass on the command line transitively.
If two maps to the same declaration file! And that can happen when they share the same name! Or refer back to the declaration file itself! Imported from a file! Through a bad import (like in my case)! Or that dist is not ignored!
Looking for imports is a thing to check! If you excluded the dist
directory and set up the outDir
correctly
Very interesting to know you can check the inputs files through this command:
tsc --listFiles
When the problem reside you'll find the declaration file within the list!
Adding "outDir": "./dist" to compilerOptions in tsconfig.json worked for me when I got this error. I'm pretty sure this is only the Visual Studio Code TypeScript extension outputting this error. I use the ts-loader with Webpack and not the tsc compiler directly so I didn't have to specify the outDir because the webpack config controls that but if this makes the VS Code extension happy it's good.