Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to export constructor in a declare module

Tags:

typescript

I want use inline-style-prefixer like:

var InlineStylePrefixer = require('inline-style-prefixer');

...

var prefixer = new InlineStylePrefixer(userAgent);


...
var _style = InlineStylePrefixer.prefixAll(style);

how to export constructor in a declare module?

declare module "inline-style-prefixer"{
    export function InlineStylePrefixer(useagent:string):void; // error
    export function prefixAll(style:Object):Object;
}
like image 931
Zhaowei Avatar asked Dec 20 '15 18:12

Zhaowei


People also ask

How do I export a constructor from TypeScript?

In order to export the default constructor, we use an export statement and import module at the required place. On creating an instance of the class, the constructor of a respective class is invoked. In the following example, we will use the JavaScript module in another module by exporting and importing it.

How do I export a TS file from a class?

Use named exports to export multiple classes in TypeScript, e.g. export class A {} and export class B {} . The exported classes can be imported by using a named import as import {A, B} from './another-file' . You can have as many named exports as necessary in a file.

How do I export a TypeScript model?

The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum. When exporting a module using export = , TypeScript-specific import module = require("module") must be used to import the module.

What can you export with module exports?

By module. exports, we can export functions, objects, and their references from one file and can use them in other files by importing them by require() method.


2 Answers

You'd do two/three declares:

declare class InlineStylePrefixer {
  constructor(useagent: string) {}
}
declare module InlineStylePrefixer {
    export function prefixAll(style: Object): Object;
}
declare module "inline-style-prefixer" {
  export = InlineStylePrefixer;
}

When you have a class and a module with the same name, the module is merged with the class.

like image 165
Louay Alakkad Avatar answered Oct 20 '22 00:10

Louay Alakkad


I'm using TypeScript 2.0.10

Here's my definition that works for FuzzySearch, which I think is similar to what you want.

The default export is the class, which applies to this kind of import:
import * as FuzzySearch from 'fuzzy-search';

The trick is to declare the class and also a variable with the class as the value and make that variable the default export.

fuzzy-search.d.ts

interface FuzzyOptions {
    caseSensitive?: boolean;
    sort?: boolean;
}

declare class FuzzySearch<T> {
    constructor(haystack: T[], keys: string[], options?: FuzzyOptions);
    search(needle: string): T[];
}

declare var fuzzySearchClass = FuzzySearch;

declare module 'fuzzy-search' {
    export = fuzzySearchClass;
}

If you import the class from the module, such as:

import { Thing } from 'thing-module';

thing-module.d.ts

declare module 'thing-module' {
  declare class Thing {
    constructor(someArg: number);
    doSomething(): string;
  }
}
like image 29
Arlo Avatar answered Oct 19 '22 23:10

Arlo