"Could not find a declaration file for module", custom declaration file in typeRoots folder is ignored
Asked Answered
H

0

5

Hi I am stuck with a TypeScript problem. I have the following directory structure:

  • package.json: the top project
  • node_modules/: all installed libraries
  • tsconfig.json
  • build/: a generated folder
    • index.js
    • handler.js
    • ...
  • server/: a folder with a TypeScript server built on top of the build folder
    • typings/
      • handler/
        • index.d.ts
    • tsconfig.json:
    • index.ts: a TypeScript server

The file server/tsconfig.json contains the lines

    "compilerOptions": {
        "typeRoots": [
            "../node_modules/@types",
            "./typings"
        ],
    ...
    "include": [
        "./*.ts",
        "./typings/**/*.ts"
    ],

I import an un-typed JavaScript function in TypeScript in the file server/index.ts:

import { handler } from '../build/handler.js'
... (code using handler) ...

The file server/typings/handler/index.d.ts contains

declare module 'handler' {
    export const handler: (a: any) => void 
}

But this declaration file is not picked up by tsc. Running npx tsc --project server/tsconfig.json" gives

server/index.ts(6,25): error TS7016: Could not find a declaration file for module '../build/handler.js'. '/home/..../build/handler.js' implicitly has an 'any' type.

Adding --traceResolution shows that the build/handler.d.ts file location is checked, but the typings/handler/index.d.ts location is not checked.

I have read Could not find a declaration file for module 'module-name'. '/path/to/module-name.js' implicitly has an 'any' type but this does not mention a relative import.

I tried Could not find declaration file with custom declaration file for 'react-dates' module but it doesn't help.

How can I make TypeScript detect the handler.d.ts file in the typeRoots directory?

Hedges answered 30/11, 2022 at 11:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.