I am currently facing an issue with my Typescript library that I am trying to publish on npmjs. It seems like the types file is not being exported correctly.
The library has a simple method in the src/index.ts
file and typings from src/typings/index.d.ts
. (with some function names/parameters changed)
import { CoolData } from "./typings";
export const sampleExport = async (): Promise<CoolData[]> => {}
This is the content of my tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
"resolveJsonModule": true,
"lib": ["es6"],
"declaration": true,
"declarationMap": true,
"declarationDir": "dist",
"outDir": "dist",
"rootDir": "src",
"strict": true
},
"include": ["src"],
"exclude": ["node_modules", "**/*.spec.ts", "dist"]
}
and part of the package.json
"main": "dist/index",
"types": "src/typings/index.d.ts",
"files": [
"dist",
"src/typings/index.d.ts"
],
After generating the index.d.ts
inside the dist
folder, it references the wrong path for the typings.
import { CoolData } from "./typings";
// Cannot find module './typings' or its corresponding type declarations.
Despite this issue, I am still able to publish and use the library. However, the problem is that it returns an any
type for the method.
await sampleExport([]); // return type Promise<any>
I have been struggling to resolve this problem for quite some time now. Any assistance would be greatly appreciated. Thank you.
Edit:
If I let Typescript auto-generate my typings, it creates this in dist/index.d.ts
with the same error
import { CoolData } from "./typings";
// Cannot find module './typings' or its corresponding type declarations.
export declare const sampleExport: () => Promise<CoolData[]>;
//# sourceMappingURL=index.d.ts.map