It’s not possible for Typescript to reach an exported function in a different module

Having trouble referencing and using exported methods from another module. I keep getting an error that says 'There is no exported member in SecondModule'.

module FirstModule{
    export class someClass{
         constructor(method: SecondModule.exMethod)   //Error occurs here: 'There is no exported member in SecondModule'
         {}
    }
}

module SecondModule{
    export function exMethod(){
        return function(input){
             //perform some actions
             return result;
        }
    }

}

Answer №1

It is not possible to use a function reference as a type directly, but you can limit the constructor to a specific function type that accepts functions with a signature similar to exMethod.

Check out this code snippet for an example:

module FirstModule {
    export class MyClass {
         constructor(method: () => (input) => any) {
         }
    }
}

module SecondModule {
    export function exMethod() {
        return function(input) {
             // do something
             return result;
        }
    }
}

new FirstModule.MyClass(SecondModule.exMethod); // This works fine
new FirstModule.MyClass(() => {});              // This will throw an error

If you absolutely need to ensure that only SecondModule.exMethod is passed in, you can skip the parameter and call the function directly within MyClass:

module FirstModule {
    export class MyClass {
         constructor() {
             SecondModule.exMethod()(5); // Example of calling the method
         }
    }
}

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

When considering Angular directives, which is more suitable for this scenario: structural or attribute?

In the process of developing an Angular 5 directive, I aim to incorporate various host views (generated from a component) into the viewContainer. However, I find myself at a crossroads as to whether I should opt for an attribute directive or a structural ...

Passing a string to a function in AngularJS through ng-click

How can I pass a string to a function using ng click? Here's an example: HTML <a class="btn"> ng-click="test(STRING_TO_PASS)"</a> Controller $scope.test = function(stringOne, stringTwo){ valueOne = test(STRING_TO_PASS); } Edit - ...

Modifying Arrays with Different Data Structures in JavaScript

I am interested in implementing the following scenario: var A = ["Jon","Brad","Rachel"]; var B = ["Male","Male","Female"]; var C = [ {"Jon","Male"}, {"Brad","Male"}, {"Rachel","Female"} ] Can you guide me on how to retrieve var C using javascrip ...

Material-UI React Native components: Injecting Styles without Props

import {createStyles, WithStyles} from "@material-ui/core"; const styles = (theme: Theme) => createStyles({ root: {} }); interface MyProps extends WithStyles<typeof styles> { } export class MyComponent extends Component<MyProps ...

What is the process for obtaining the feedback from a new StepFunctionsStartExecution operation using AWS CDK?

Task Explanation: Iterate through the children in Step Function 1 Forward each JSON object to Step Function 2 Upon completion of Step Function 2, a response is obtained from an external API Utilize this API response within Step Function 1 Visual Represen ...

Is there a method to incorporate a click event for the confirm button in the ElMessageBox UI element?

When I try to remove data from the table, I need a warning message to appear in the center of the screen first. The delete function is already set up, but I'm struggling to figure out how to implement a confirm button click event with ElMessageBox. I ...

The most basic Angular module configuration

What is needed to prevent the "no module: tut" error from occurring? <!DOCTYPE html> <html ng-app="tut"> <head> <script type="text/javascript" src="./jquery.js"></script> <script type="text/javascript" ...

What is the best method for passing string data to a SQL query?

I designed a basic login form for Username and Password in Visual Studio using C# and it works well. I managed to save whatever inputs were entered into the textboxes as string values. However, now I am looking to pass these strings as parameters and sto ...

What is the solution to preventing Angular 1.3 ngMessages from affecting the size of my form?

Hey there, I'm diving into front-end design for the first time. My issue is with a form that I want to use ng-messages for validation error messages. Currently, my form's size changes depending on whether there are errors displayed or not, and it ...

transformation of categorized unions in software development

Experimenting with routing-controllers and its built-in class-transformer feature, I tried creating an interface for executing a search query based on either a location id or location coordinate. My aim was to utilize a discriminated union as a body parame ...

Converting Typescript fat arrow syntax to regular Javascript syntax

I recently started learning typescript and I'm having trouble understanding the => arrow function. Could someone clarify the meaning of this JavaScript code snippet for me: this.dropDownFilter = values => values.filter(option => option.value ...

The SortKey<> function is ineffective, even though the individual types it uses work perfectly fine

After my initial inquiry about TypeScript, the focus shifted to this current topic. In my previous question, I was attempting to develop a generic sort() method that could function with an array of sort keys defined by the SortKey type featured there (and ...

How can I set a document ID as a variable in Firebase Firestore?

I recently set up a firestore collection and successfully added data (documents) to it. Firestore conveniently generates unique document ids for each entry. Inserting Data this.afs.collection('questions').add({ 'question': message, &a ...

standards for matching patterns (such as .gitignore)

Throughout my experience, I have utilized various tools designed to search a codebase for specific files and then carry out operations on those files. One example is test libraries that identify all the necessary files for execution. Another common tool is ...

Having trouble with installing NPM on Ubuntu?

I am trying to set up npm for using angular2 with laravel5.2, but encountered an issue during the installation process. Here is the error message I received: npm WARN npm npm does not support Node.js v0.10.25 npm WARN npm You should probably upgrade to a ...

Using TypeScript to ensure class parameter types without affecting properties

I am tasked with defining a schema for "operations" that will be used in my application. This schema must be easily extendable for other groups of "operations" and should include a dictionary of settings for each keyword. Eventually, a "generic caller" wi ...

What specific files from the Kendo core are required for utilizing Mobile and Angular functionalities?

After browsing through similar questions, I couldn't find a solution. Currently, I am experimenting with Kendo (open source core for now) in a Visual Studio Cordova project. Initially, disregarding Cordova, I am focusing on setting up a basic view wit ...

Creating a custom Map type in TypeScript

I am exploring the concept of defining a Map type in Typescript using generics. Essentially, I want to create something similar to: EntityMap<U, V>, where U can only be either a string or a number This is what I have managed to come up with so far: ...

What's causing the subscription feature to malfunction in a fresh browser tab?

I am facing an issue with camera entries on an angular website. Whenever I click on an entry, a new window opens to display the camera livestream. However, I am having trouble with the subscribe functionality. Important note: Once the window is open, subs ...

Advantages of using $apply(fn) instead of $apply() in AngularJS

Currently, I am exploring a blog post about Angular tips that discusses how the $apply function runs a digest cycle. You can check it out here: The blog also explains a better way to use $apply in a directive link function: element.bind('click' ...