error TS2304: Cannot find name 'SomeCustomType' while trying to use custom declarations for js modules with no types.
See original GitHub issueHello,
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:
- Created 2 years ago
- Comments:8 (4 by maintainers)
Top GitHub Comments
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!
I’m going to close the issue. If you’ll have any question or updates - feel free to ping me.