how to export constructor in a declare module

后端 未结 2 2006
轻奢々
轻奢々 2021-02-13 18:57

I want use inline-style-prefixer like:

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

...

var prefixer          


        
相关标签:
2条回答
  • 2021-02-13 19:25

    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;
      }
    }
    
    0 讨论(0)
  • 2021-02-13 19:26

    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.

    0 讨论(0)
提交回复
热议问题