question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

error TS2304: Cannot find name 'SomeCustomType' while trying to use custom declarations for js modules with no types.

See original GitHub issue

Hello,

I have a node typescript project, below is my tsconfig.json at the root of my project:

<tsconfig.json>

{
    "compilerOptions": {
        "incremental": true,
        "target": "es2017",
        "outDir": "dist",
        "rootDir": "src",
        "moduleResolution": "node",
        "module": "commonjs",
        "declaration": true,
        "declarationDir": "./types",
        "declarationMap": true,
        "inlineSourceMap": true,
        "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
        "resolveJsonModule": true /* Include modules imported with .json extension. */,
        /* Additional Checks */
        "noUnusedLocals": true /* Report errors on unused locals. */,
        "noUnusedParameters": true /* Report errors on unused parameters. */,
        "noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
        "noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,
        /* Debugging Options */
        "traceResolution": false /* Report module resolution log messages. */,
        "listEmittedFiles": false /* Print names of generated files part of the compilation. */,
        "listFiles": false /* Print names of files part of the compilation. */,
        "pretty": true /* Stylize errors and messages using color and context. */,
        "lib": ["es2015", "DOM"],
        "types": ["node"],
        "typeRoots": ["node_modules/@types", "src/types"]
    },
    "include": ["src/**/*.ts", "types/self_delarations.d.ts"],
    "exclude": ["node_modules/**"],
    "compileOnSave": true
}

I have exported all declaration files and their maps in types folder which is at the root of my project

As you can see I’ve added a file (types/self_delarations.d.ts) in include array in the above config. this file has types that I add for imported js libraries with no types of their own. here is the contents of self_declarations.d.ts:

<self_delarations.d.ts>

declare module 'my-awesome-js-library' {
    type SomeCustomType = string | boolean;
    const someFunction: () => SomeCustomType;

    export { someFunction, SomeCustomType };
    export default someFunction;
}

The library has one index.js file with the following content:

<node_modules/my-awesome-js-library/index.js>

function someFunction() { return false };

try {
	module.exports = someFunction;
} catch (e) {
	String.someFunction = someFunction;
}

I am consuming this library just fine in my project.

However, I am using dts-bundle-generator as a post build process by running:

dts-bundle-generator -o ./dist/bundle.d.ts ./types/index.d.ts

this tries to generate bundle.d.ts in my dist folder, and generates the following content :

export declare const data2: SomeCustomType;

and this is causing an error: dist/bundle.d.ts(1,29): error TS2304: Cannot find name 'SomeCustomType'.

How can I resolve this issue? I want to be able to add custom types in my types/self_declarations.d.ts file for modules that don’t have their own types.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Rushcommented, Jul 14, 2022

I have a similar issue. I understand a minimal repro is needed. If I don’t find a solution I’ll try to create one but it may be difficult for more complex projects. I understand it may be difficult to guess what’s wrong without a repro. Anyhow, FYI!

0reactions
timocovcommented, Nov 13, 2021

I’m going to close the issue. If you’ll have any question or updates - feel free to ping me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript getting error TS2304: cannot find name ' require'
I am getting the error "TS2304: Cannot find name 'require' " when I attempt to transpile a simple TypeScript Node.js page. I have...
Read more >
[TypeScript] "error TS2304: Cannot find name 'moment'" when ...
Typescript seems to not be able to resolve the global moment() function when used in a non-module context. tsc version: 2.1.6 moment ...
Read more >
Using a JavaScript library (without type declarations ... - Medium
Let's say you want to use a library in your TypeScript project. ... Could not find a declaration file for module 'my-untyped-module'. '....
Read more >
Node.js best practices list (July 2021) - DEV Community ‍ ‍
2.2 Use only the built-in Error object. TL;DR: Many throw errors as a string or as some custom type – this complicates the...
Read more >
TypeScript errors and how to fix them
error TS2307: Cannot find module 'events' or its corresponding type declarations. Broken Code ❌. You are importing from a core Node.js module (e.g....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found