Amir-Mousavi
Amir-Mousavi

Reputation: 4561

Create React App cannot find declared module

I have a project create with CRA typescript.

at the root of project there exists tsconfig.json and a folder called types. at the types folder I have created a modulename.d.ts file for a module that does not have default typings at node_modules and also there is nothing for it at @types\module-name.

but I get the error at compile time.

Type error: Could not find a declaration file for module 'thename'. '/*/node_modules/thename/dist/entry.js' implicitly has an 'any' type.

why the compiler does not find the type declaration at types folder?

// .d.ts
declare module 'foo-react' {
    import * as React from 'react';
    // ...
}

this is the tsconfig.json

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": { "*": ["types/*"] },
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "plugins": [{ "name": "typescript-tslint-plugin" }],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "preserve"
  },
  "include": ["src"]
}

Upvotes: 7

Views: 3785

Answers (1)

Amir-Mousavi
Amir-Mousavi

Reputation: 4561

So it turned out that

  1. I renamed the custom types folder to @types
  2. the path to @types folder (my customized one at the root of the project) should be added to types attribute and not the paths attribute. "types":["./@types"]
  3. In the @types folder having a structure like @types/<module_name>/index.d.ts or @types/moduleName.d.ts. Now the file can declare that module as any or declare all types in detail.

definition for types compiler option in Typescript handbook

--types string[] List of names of type definitions to include. See @types, —typeRoots and —types for more details.

Upvotes: 4

Related Questions