Having trouble locating the custom interface name in a TypeScript custom npm package?

I have an Angular application in TypeScript that is built with webpack. I also have an npm package (hosted in Sinopia, not on npm) structured as follows:

my-custom-npm-package
- index.ts
- studentStorage.ts
- student.d.ts

student.d.ts

interface IStudent
{
    name: string;
    id: number;
}

index.ts

import { StudentStorage } from './studentStorage';
export { StudentStorage }

studentStorage.ts

export class StudentStorage{
    private students: IStudent[] = [];

    public get(): IStudent[]{
        return this.students.slice(0);
    }   
}

Within my Angular component, I include the following line of code:

import { StudentStorage } from 'my-custom-npm-package';

However, when using webpack, I encounter the following error:

ERROR in D:\Sandbox\MultiLanguage\node_modules\my-custom-npm-package\studentStorage.ts
(2,23): error TS2304: Cannot find name 'IStudent'.

To address this issue, I created a workaround by creating a typings.d.ts file with the following content:

/// <reference path="node_modules/my-custom-npm-package/student.d.ts" />

Despite this fix, I aim to utilize my custom npm package without relying on referencing .d.ts files during installation.

How can I accomplish this goal?

Answer №1

To address this issue, it is recommended to modify the structure by adding

/// <reference path="student.d.ts" />
to studentStorage.ts. However, having IStudent as a global entity while the rest of the library functions within modules may seem unconventional. An alternative approach would be to transform student.d.ts into a module by adjusting it to:

export interface IStudent
{
    name: string;
    id: number;
}

Furthermore, update studentStorage.ts as follows:

import { IStudent } from "./student";
export class StudentStorage{
    private students: IStudent[] = [];

    public get(): IStudent[]{
        return this.students.slice(0);
    }   
}

This modification will require clients utilizing your library and wanting to access IStudent to import it accordingly.

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

Issue with installing packages using node.js 9 and npm 5.5.1: encountering errors during installation process

After recently updating my project to node.js 9.1.0 (and bundle nom 5.5.1), I am encountering errors when trying to run the installation process. Even with verbose output in the logs, I am struggling to pinpoint where exactly the problem lies. Any assist ...

What is the best way to define a npm dependency using a GitHub subfolder URL?

Check out my git repository: README.md packages common package.json main package.json Can anyone advise how to specify the common dependency in the main package.json using a github subfolder URL? I tried this but it doesn't seem to be w ...

Getting started with WebTorrent: A beginner's guide

I have been brainstorming some ideas for using WebTorrent. While I am comfortable with JavaScript and jQuery, I have never ventured into Node.js or Browserify territory. Can someone guide me through how to implement the following straightforward code? var ...

Tips for setting up OpenLayers demonstrations on a personal server?

Currently, I'm working with an older version of OpenLayers (4.6.2) and find the provided examples extremely helpful for testing and reference purposes. However, the official web page with updated examples only includes the latest version. I am wonder ...

Why is it necessary to use the exports "./package.json" declaration within package.json files?

Upon reviewing certain npm modules' package.json files, it caught my attention that some contain an exports section: { "name": "my-package", "exports": { ".": "./lib/index.js", "./package.json": "./package.json" } } I am curious about t ...

Steps for utilizing a Get() method to view a response within Angular

I am having trouble with implementing an API call on a page and I'm unsure about what's wrong with the Subscribe/Observe method. Currently, this is the code that I have: import { Component, OnInit } from '@angular/core'; import { Ro ...

Specify the return type based on specific parameter value

I'm facing a situation where I have two definitions that are identical, but I need them to behave differently based on the value of the limit parameter. Specifically, I want the first definition to return Promise<Cursor<T>> when limit is g ...

What is the significance of TypeScript's dual generic typing feature?

defineListenerShape< EventName extends string, EventData extends { [key in EventName]: unknown; } > = <E extends EventName>(data: EventData[E]) => void; enum EventName { Click = 'click', Hover = 'hover' ...

Global error handling in Nest.js: A guide to applying consistent error logic throughout your application

Nest.js I successfully implemented error handling logic as required by the project. Is there a way to reuse this logic for multiple routes in a controller without duplicating the code? @Controller() export class AppController { constructor(private read ...

After filling a Set with asynchronous callbacks, attempting to iterate over it with a for-of loop does not accept using .entries() as an Array

Encountering issues with utilizing a Set populated asynchronously: const MaterialType_Requests_FromESI$ = SDE_REACTIONDATA.map(data => this.ESI.ReturnsType_AtId(data.materialTypeID)); let MaterialCollectionSet: Set<string> = new Set<s ...

Troubleshooting connectivity issues between Entities in microORM and Next.js

While trying to run my Next.js application in typescript, I encountered the following error: Error - ReferenceError: Cannot access 'Member' before initialization After consulting the documentation at https://mikro-orm.io/docs/relationships#relat ...

Issue with Next.js: Router.push not causing page to refresh

I'm currently developing a next.js page called /page/data/[dataId] (this page is accessed when a user clicks on a link from the page /page/data, where I fetch the list of items through an API). When a user clicks on a button, I make an API call to de ...

Harness the power of moment.js in webpack 4 by integrating it with ts-loader

I'm facing an issue where moment.js isn't loading inside a TypeScript file during the webpack4 build process, no matter what I attempt. [tsl] ERROR in /app/Repository/JFFH.Site/Resources/Private/JavaScript/Angular/schedule/schedule.component.ts( ...

Troubleshooting: Icon missing from React vscode-webview-ui-toolkit button

In the process of developing a VSCode extension using React and the WebUi Toolkit library for components, I encountered an issue with adding a "save" icon to my button. I diligently followed the documentation provided by Microsoft for integrating buttons i ...

Issue with Typescript npm script including compilation and nodemon issue

After exploring various SO links, I tried to find a way to simultaneously run both tsc -w and nodemon app.js using a single command. The link I referred to is: How do I execute typescript watch and running server at the same time? Working on a node.js pr ...

Creating a production-ready installation of Tailwind CSS with Vue 3

Although I primarily develop in Python, I found myself struggling with npm after trying to integrate Tailwindcss with 'Vue 3'. Following the installation steps outlined on the Tailwind+Vue 3 website, I ran into some issues. npm init @vitejs/app m ...

What is the best way to send ServerSideProps to a different page in Next.js using TypeScript?

import type { NextPage } from 'next' import Head from 'next/head' import Feed from './components/Feed'; import News from './components/News'; import Link from 'next/link'; import axios from 'axios&apo ...

VScode has identified potential problems with modules that utilize Angular Ivy, however, this should not cause any major issues

Encountering a problem with using Angular in VSCode, where there seems to be no ngModules due to AngularIvy. The error message indicates an issue with 'CommonModule' not being recognized as an NgModule class: [{ "resource": "[...]src/app/com ...

The useParams() method results in a null value

When attempting to utilize the useParams() hook in nextjs, I am encountering an issue where it returns null despite following the documentation. Here is my current directory structure: pages ├── [gameCode] │ └── index.tsx Within index.tsx ...

What advantages do binary shifts offer in enums?

What do you think about this code snippet? /** * Bitmask of states */ export const enum ViewState { FirstCheck = 1 << 0, // result is 1 ChecksEnabled = 1 << 1, // result is 2 Errored = 1 << 2, // result is 4 ...