Skip to content

Typescript Custom Declaration Files Not Resolving #13304

@adstep

Description

@adstep

Describe the bug

I'm not able to resolve custom typescript declaration files when running the project. The project compiles with tsc fine, but react-scripts reports:

Module not found: Error: Can't resolve 'MyLibrary' in 'C:\dev\github\custom-types\src'

Did you try recovering your dependencies?

Repros on latest build.

Which terms did you search for in User Guide?

Environment

Environment Info:

  current version of create-react-app: 5.0.1
  running from C:\Users\adstep\AppData\Local\npm-cache\_npx\c67e74de0542c87c\node_modules\create-react-app

  System:
    OS: Windows 10 10.0.25393
    CPU: (16) x64 Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz
  Binaries:
    Node: 19.2.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - C:\Program Files\nodejs\yarn.CMD
    npm: 9.8.1 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 115.0.5790.102
    Edge: Spartan (44.25393.1.0), Chromium (115.0.1901.183)
    Internet Explorer: 11.0.25393.1
  npmPackages:
    react: ^18.2.0 => 18.2.0
    react-dom: ^18.2.0 => 18.2.0
    react-scripts: 5.0.1 => 5.0.1
  npmGlobalPackages:
    create-react-app: Not Found

Steps to reproduce

Starting from a newly minted app using the typescript template I....

  1. Added a custom definition file under src/types/MyLibrary/index.d.ts:
declare module 'MyLibrary' {
    export function Sum(a: number, b: number);
}
  1. Updated tsconfig.json to define typeRoots:
"typeRoots": [
      "node_modules/@types", 
      "src/types"
    ],
  1. Added a reference to the custom definition in src\App.ts.

Expected behavior

App compiles without any errors.

Actual behavior

Error logs:

src\App.tsx
  Line 7:12:  'DoSomething' is defined but never used  @typescript-eslint/no-unused-vars

ERROR in ./src/App.tsx 7:0-32
Module not found: Error: Can't resolve 'MyLibrary' in 'C:\dev\github\custom-types\src'

webpack compiled with 1 error and 1 warning
Files successfully emitted, waiting for typecheck results...
Issues checking in progress...
No issues found.

Reproducible demo

I have created a repo that demonstrates the issue see custom-types.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions