I am trying add typings for our Javascript files at work while we are in the process of converting to Typescript. However, I can not get the declaration files to be recognized.
Here is my file structure
- js
- Foo.js
- typings
- Foo
- index.d.ts
- Foo
- index.ts
- package.json
- tsconfig.json
Foo.js
module.exports = function Foo() {
return 'Bar';
};
index.d.ts
export = Foo;
declare function Foo(): string;
index.ts
import Foo = require('./js/Foo')
console.log(Foo());
tsconfig.json
{
"compilerOptions": {
"typeRoots": ["./typings"],
"target": "es5",
"strict": true,
"baseUrl": "./",
"paths": {
"*": ["typings/*"]
}
}
}
package.json
{
"name": "fail",
"version": "1.0.0",
"description": "",
"main": "index.ts",
"scripts": {
"tsc": "tsc"
},
"author": "",
"license": "MIT",
"dependencies": {
"typescript": "^3.1.4"
}
}
Here is repo to reproduce my problem
https://github.com/erisman20/typings_help
Edit: Here is there error I get
error TS7016: Could not find a declaration file for module './js/Foo.js'. '....../js/Foo.js' implicitly has an 'any' type.
The only ways to provide declarations for a relative import such as
'./js/Foo'are to actually have the declaration file at the import path (plus.d.tsor/index.d.ts) or to have it virtually at the import path based on yourrootDirsoption. NeithertypeRootsnorbaseUrl/pathscomes into play in this scenario:baseUrl/pathsonly affects module resolution for "non-relative" paths, andtypeRootscan be used to get TypeScript to load files but does not affect the relationship between import paths and files at all.The simplest solution would be to put a
Foo.d.tsfile in the same directory asFoo.js. If you want to keep the typings in a separate directory, then you can add"rootDirs": ["js", "typings"]totsconfig.json. That change is enough to make your example work for me, though I find it confusing to have correspondingFoo.jsandFoo/index.d.tsfiles and would encourage you to be consistent in your use of subdirectories, i.e., either switch toFoo/index.json the JavaScript side or switch toFoo.d.tson the TypeScript side.