The path referenced in typings is incorrect

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

Answer №1

To successfully export a function declaration, simply add it to the src/typings/index.d.ts file.

export declare function exportedSample(): Promise<NiceData[]>;

Don't forget to remove the mentions of declaration and declarationMap from your tsconfig.json.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Emphasize a Row Based on a Certain Criteria

One of the challenges I am facing is how to emphasize a specific row in a table based on certain conditions. Currently, I am utilizing Jqxgrid and have made some modifications in the front-end to achieve the highlighting effect: TypeScript: carsDataAgain ...

Infinite Loop Issue in Angular2 RC5 when using the templateUrl

Encountering an issue with Angular2 RC5 that seems to be causing an infinite loop problem. The app.component, which is bootstrapped by the app.module, appears quite simple: @Component({ selector: 'my-app', template: `TEST` }) export cl ...

What caused the error when trying to create a React App?

I recently decided to delve into learning ReactJS and started by creating a new empty folder using the npx create-react-app . command. However, I encountered an error despite having npm v.6.4.1. Error: EPERM: operation not permitted, mkdir 'C:\U ...

The component is expected to return a JSX.Element, however it is failing to return any value

The issue lies with this component: const NavigationItems = (props: {name: string, href: string}[]): JSX.Element => { props.map((item, index) => { return <a href={item.href} key={index}>{item.name}</a> }) }; export default Naviga ...

Are generic constraints leading to type inference selecting the incorrect candidate?

TypeScript Version: 2.6.0-dev.20170826 and 2.4.2 I'm questioning whether I've encountered a TypeScript inference bug or limitation, or if my code is simply incorrect. If the code is valid and it's an issue with type inference, I will repor ...

Refresh the Angular view only when there are changes to the object's properties

I have a situation where I am fetching data every 15 seconds from my web API in my Angular application. This continuous polling is causing the Angular Material expansion panel to reset to its default position, resulting in a slow website performance and in ...

Why am I encountering the error message "Error: Cannot locate module './keywords'" when trying to run npm start?

I attempted to run npm start using Create React App to launch my react application, but encountered the following error: node:internal/modules/cjs/loader:943 throw err; ^ Error: Cannot find module './keywords' Require stack: - D:\social ...

Performing Cypress testing involves comparing the token stored in the localStorage with the one saved in the clipboard

I am currently working on a button function that copies the token stored in localStorage to the clipboard. I am trying to write code that will compare the token in localStorage with the one in the clipboard in order to verify if the copy was successful. H ...

Should I fork and customize npm package: Source or Distribution? How to handle the distribution files?

Currently, I am in the process of developing a VueJS web application. Within this project, there is a module that utilizes a wrapper for a JavaScript library obtained through npm and designed to seamlessly integrate with VueJS. However, it doesn't com ...

Loading dynamic components asynchronously in Vue 3 allows for improved performance and enhanced user experience

My goal is to dynamically display components based on their type. Here's how I'm approaching it: I have several similar components that should show different content depending on their type. Using the defineAsyncComponent method, I can easily im ...

Encountering an error with the node module timestampnotes: 'command not recognized'

I am encountering an issue while trying to utilize a npm package called timestamp notes. After executing the following commands: $npm install timestampnotes $timestamp I receive the error message: timestamp:126: command not found: slk Subsequently, I ...

Is it possible to automate the process of constructing a dependency in the package.json file?

Currently, I am using firebaseui and require building it with French localization because the localized versions are not available on npm. In my current package.json file, the dependencies section looks like this: "dependencies": { "firebaseui": "^3.5 ...

Converting typescript path aliases into local file paths

Just dipping my toes into typescript and grappling with module resolution. The problem seems straightforward (or so I think), but there's something off about my tsconfig.json. If my folder structure looks like this: + release + definitions + ...

Eliminating an index from a JSON array using Typescript

I'm working with a JSON array/model that is structured as follows: var jsonArray = [0] [1] ... [x] [anotherArray][0] [1] ... [e] My goal is to extract only the arrays from [0] to [x] and save them into their ...

Execute a function when a button is pressed in a React application

Currently, I am dynamically generating some HTML and have a requirement for certain "events" to trigger an onclick function. The technology stack I am using for this project involves React and TypeScript. My initial approach is as follows: function add_ev ...

Incorporate a stylish gradient background into react-chartjs-2

I am currently working on adding a gradient background with some transparency to a radar graph within a react component. So far, I have only found solutions that involve referencing a chartjs canvas in an html file, but none for implementing it directly in ...

Tips for specifying the type when utilizing the spread operator to pass props

type TypeData = { data: { id: string; class: string; name: string; country: string; ew_get_url: string; ew_post_url: string; rocket_id: string; pages: { landing: { h1: string; h2: string; } ...

Encountering a "args" property undefined error when compiling a .ts file in Visual Studio Code IDE

I've created a tsconfig.json file with the following content: { "compilerOptions": { "target": "es5" } } In my HelloWorld.ts file, I have the following code: function SayHello() { let x = "Hello World!"; alert(x); } However ...

Expanding a Zod object by merging it with a different object and selecting specific entries

Utilizing Zod, a TypeScript schema validation library, to validate objects within my application has led me to encounter a specific scenario. I find myself in need of validating an object with nested properties and extending it with another object while se ...

Combining Rxjs map and filter to extract countries and their corresponding states from a JSON dataset

I have a unique dataset in JSON format that includes information about countries and states. For example: { "countries": [ { "id": 1, "name": "United States" }, { "id": 2, "name": "India" }], "states": [ { ...