7

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"]
}

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.