I am trying to write TypeScript typings for a JavaScript module in a TypeScript subproject. My project structure looks like this
tsconfig.json
server
tsconfig.json
index.ts
global.d.ts
build
handler.js
- more compiled files
In the file server/index.ts
, I wrote:
import { handler } from '../build/handler.js'
In server/tsconfig.json
, I wrote
"include": [
"./*.ts"
],
I have to store the .d.ts file outside the build
folder because the build folder is overwritten with each build. According to Could not find a declaration file for module 'module-name'. '/path/to/module-name.js' implicitly has an 'any' type, I wrote a file server/global.d.ts
with contents
declare module "handler" {
export function handler(arg: any): any;
}
Running npx tsc --project server/tsconfig.json
I get the error:
Could not find a declaration file for module '../build/handler.js'. '/absolute_path/build/handler.js' implicitly has an 'any' type.",
It seems like the global.d.ts
file is not picked up. How should I name the global.d.ts
file and what should the contents be?
I did change the module name to a relative path, but that gives an error about ambient modules.
Furthermore, I have tried changing the name according to answer to Typescript: .d.ts file not recognized. But it doesn't work.
(I would rather not use // @ts-ignore.
)