What is preventing me from getting this typescript plugin to function properly?

Currently, I am working on developing a plugin example and facing an issue. I am struggling to make the module loader recognize that the plugin has additional functionality compared to the original. I believe I am close to a solution, but a little nudge in the right direction would be greatly appreciated.

If you are interested, you can find the plugin example code on GitHub here.

Within my main file named Communicator.ts, I have the following code snippet:

    import * as communicatorModularAMD from "communicatorModularAMD";

    class Communicator {
        constructor() { }
        greet(message: string) {
            return communicatorModularAMD.goodbye();
        }
    };

    var communicator = new Communicator();

    document!.body.innerHTML = communicator.greet("Hello, world");

Essentially, CommunicatorModularAMD is a JavaScript library I have developed and integrated into this project. My goal is to showcase how to create a plugin for it, hence the following code snippet:

define(['CommunicatorModularAMD'], function() {
    return {
        goodbye: function() {
            return "<h1>Goodbye!</h1>";
        }
    };
});

Alongside this, I have a declaration file with the following content:

import * as m from 'communicatorModularAMD';

declare module 'communicatorModularAMD' {
    export function goodbye(): string;
}

Although the TypeScript compiles without any errors, the "goodbye" function is not found when I attempt to run the application. Upon inspecting the console.log output of the communicatorModularAMD object, it becomes evident that "goodbye" is missing.

Answer №1

It seems that while you cannot assign top-level properties to an imported module, you are able to define nested properties such as module.plugin.custom

app/Communicator.ts

import * as communicatorModularAMD from "communicatorModularAMD";
import * as communicatorPlugin from "../communicatorPlugin";

console.log(communicatorPlugin);
console.log(communicatorModularAMD);

class Communicator {
    constructor() { }
    greet(message: string) {
        return communicatorModularAMD.plugin.goodbye();
    }
};

var communicator = new Communicator();

document!.body.innerHTML = communicator.greet("Hello, world");

communicatorModularAMD/CommunicatorModularAMD.js

define([], function () {
    return {
        plugin: {},
        greet: function (message) {
            return "<h1>" + message + "</h1>";
        }
    };
});

communicatorModularAMD/index.d.ts

declare module communicatorModularAMD {
    interface pluginList { }
    export var plugin: pluginList;
    export function greet(message: string): string;
} 

communicatorPlugin/index.js

import 'communicatorModularAMD';

declare module 'communicatorModularAMD' {
    interface pluginList {
        goodbye(): string;
    }
}

communicatorPlugin/index.d.ts

define(['communicatorModularAMD'], function (communicatorModularAMD) {
    communicatorModularAMD.plugin.goodbye = function () {
        return "<h1>Goodbye!</h1>";
    }
    return communicatorModularAMD;
});

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

Ensuring TypeORM constraint validations work seamlessly with MySQL and MariaDB

I recently started using TypeORM and I'm trying to incorporate the check decorator in my MySQL/MariaDB database. However, after doing some research on the documentation and online, it seems that the check decorator is not supported for MySQL. I'v ...

Issues with Ionic 3 Directive Not Functioning

Struggling to create a custom directive in Ionic that won't resize automatically? I can't figure out what's going wrong. Here's the code snippet from my project, which is an Ionic 3 app with Angular 4: import { Directive, HostListener ...

Is there a way to transform time into a percentage with the help of the moment

I am looking to convert a specific time range into a percentage, but I'm unsure if moment.js is capable of handling this task. For example: let start = 08:00:00 // until let end = 09:00:00 In theory, this equates to 100%, however, my frontend data ...

What is the purpose of the tabindex in the MUI Modal component?

Struggling with integrating a modal into my project - it's refusing to close and taking up the entire screen height. On inspection, I found this troublesome code: [tabindex]: outline: none; height: 100% How can I remove height: 100% from the ...

Angular displays incorrect HTTP error response status as 0 instead of the actual status code

In Angular, the HttpErrorResponse status is returning 0 instead of the actual status. However, the correct status is being displayed in the browser's network tab. ...

Using Typescript for AngularJS bindings with ng.IComponentController

Currently, I am utilizing webpack alongside Babel and Typescript Presently, the controller in question is as follows: // HelloWorldController.ts class HelloWorldController implements ng.IComponentController { constructor(private $scope: ng.IScope) { } ...

The function Observable.timer in Angular rxjs is throwing an error when imported

Encountering an error in my Angular application that reads: ERROR TypeError: rxjs_Observable__WEBPACK_IMPORTED_MODULE_4__.Observable.timer is not a function at SwitchMapSubscriber.project (hybrid.effect.ts:20) at SwitchMapSubscriber.push ...

How to send parameters with the fetch API

After completing a task that involved converting code from Angular HttpClient to using fetch API, I encountered an issue with passing parameters. Below is the original code snippet before my modifications: let activeUrl = new URL(this.serverAddress); ...

Getting permission for geoLocation service on iOS in Ionic: A step-by-step guide

I have recently developed a social media application that utilizes geoLocation services. The app is built with Ionic 4 and has a Firebase backend. While the GeoLocation services are functioning properly on Android devices, users of iOS are not being prompt ...

Visual Studio is refusing to highlight my code properly, intellisense is failing to provide suggestions, and essential functions like go to definition are not functioning as expected

Due to a non-disclosure agreement, I am unable to share any code. However, I am experiencing an issue with Visual Studio not highlighting my code or allowing me to utilize its built-in tools. While I can rebuild the project, I cannot edit or access any fil ...

Separate the date format string into tokens

I am currently attempting to create a function that is able to transform a date format string such as %d/%m/%Y %H:%n where the variables are always denoted by a percentage sign followed by one character, into an array of tokens: ["%d", "/", "%m", "/", " ...

Filter multiple columns in an Angular custom table with a unique filterPredicate

Looking to develop a versatile table that accepts tableColumns and dataSource as @Input(). I want the ability to add custom filtering for each table column. Currently, I've set up the initialization of the table FormGroup and retrieving its value for ...

"Learn how to utilize Angular to showcase an array of strings and choose a specific value for

When working in HTML, I have the ability to set the option text and send the value like this: <select id="cars"> <option value="volvo">Volvo</option> <option value="saab">Saab</option> </select> After sending it ...

What are the reasons for the inability to send form-data in Postman?

Encountering an issue when trying to send form-data in postman as Sequelize returns an error: value cannot be null However, everything works fine when sending a raw request with JSON. Have tried using body-parser and multer, but no luck. This is my inde ...

Implementing JavaScript Code in TypeScript

Every JavaScript code should also be valid in TypeScript, but when attempting to run the following code snippet below, an error is triggered. Could someone convert this JavaScript code into TypeScript? Error: 20:9 - TS2531 Error: Object is possibly 'z ...

Lazy loading implemented with BootstrapVue's b-nav component

Having some trouble wrapping my head around the following issue: I've created a Vue.js component with tabs that have routes. I opted for a variation of the b-nav Tabs style (official docs) and it's functioning well in terms of tabs and routing. ...

Encountering issues with Typescript when providing parameters for res.status().json()

I've recently started using Typescript and I'm still in the learning process by converting some existing JS code to TS. In my code: res.status(200).json({ user: data.user }) I encountered a red squiggly underline under user:data.user ...

Unlocking the Potential of Vue Class Components: Exploring Advanced Customization Options

Currently, I am working on a project using Vue 2 with Typescript. However, I am facing an issue where I cannot add options to the component. <script lang="ts"> import { Component, Vue } from "vue-property-decorator"; import HelloW ...

There appears to be a problem with the syntax in the JSON data at position 0, as it is detecting

WARNING: core.es5.js?0445:1084 WARNING SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous> SCENARIO: The goal is to automatically select the option that the user previously chose and prevent them from voting twi ...

Can someone guide me on identifying the type of object in React/Typescript?

Can anyone help me remove this unnecessary definition? const [nextLocation, setNextLocation] = useState(null) as any[]; I have been struggling with this in my React Router 6 project. I've looked through the documentation but haven't found a suit ...