Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare an external module function

Tags:

typescript

I'm trying to declare an external module which has no existing typings, but am missing something.

The library exports a function which takes no arguments and returns a string.

I'm trying to define it using this in a .d.ts file:

declare module "cuid" {
    export function cuid(): string;
}

In my code, I have import * as cuid from 'cuid';

Yet on the line where I use it, cuid() I get an error:

error TS2349: Cannot invoke an expression whose type lacks a call signature.

like image 640
helion3 Avatar asked Jul 07 '16 05:07

helion3


People also ask

What does declare module do in TypeScript?

The TypeScript declares module is one of the modules and keyword it is used for to surround and define the classes, interfaces; variables are also declared it will not originate with the TypeScript like that module is the set of files that contains values, classes, functions/methods, keywords, enum all these contains ...

What is a module declaration?

3.2. 9 MODULE declarations. A module is an encapsulated collection of declarations. Once defined, a module can be reused as many times as necessary. Modules can also be so that each instance of a module can refer to different data values.

What is the difference between the internal module and the external module?

Internal modules are declared using ModuleDeclarations that specify their name and body. A name path with more than one identifier is equivalent to a series of nested internal module declarations. External modules (section 9.4) are separately loaded bodies of code referenced using external module names.

What are modules in C++?

Modules are the independent unit of logic that consists of several functionalities, like a business logic procession, a static user interface, or full-fledged components. External modules are handy to export and import in the component using the import statement. They could be consumed quickly.

How do I import external modules into my App?

Whether it’s an independent script file or a third-party library, there are several ways of importing external modules, and below are possible ways to do that. Any class/module could be imported by providing its location from the existing app. The module can also be imported by the default name, followed by the module location.

How do I use external functions in a script?

You can use external functions that are written in any language that supports dynamic libraries. Before you can use an external function in a script, you must declare it as one of two types: These are available anywhere in the application. These are defined for a particular type of window, menu, user object, or user-defined function.

What is external method and external function in Java?

If the definition of the method written outside the body of the class then the method is called an external method. external function. The definition of the function written outside the class body is referred to as an external function


1 Answers

Use the definition with export function cuid

This syntax matches with your declaration:

import {cuid} from 'cuid';

Here is a good introduction to ES6 modules.

Or use a definition with export =

Try:

declare module "cuid" {
  function cuid(): string;
  export = cuid;
}

... Then use it: import cuid = require('cuid').

Here is the documentation.

like image 64
Paleo Avatar answered Nov 12 '22 23:11

Paleo