简体   繁体   English

是功能且具有名称空间的模块的打字稿定义?

[英]Typescript definition for a module that is a function and has namespaces?

I am trying to come up with a *.d.ts file for react-grid-layout . 我正在尝试为*.d.ts -grid-layout提出一个*.d.ts文件。 As its index.js file shows it exports a function - a subclass of React.Component called ReactGridLayout : 如其index.js文件所示,它导出一个函数React.Component的子类,称为ReactGridLayout

// react-grid-layout/index.js
module.exports = require('./build/ReactGridLayout').default;
module.exports.utils = require('./build/utils');
// ...

Requiring: 要求:

var ReactGridLayout = require('react-grid-layout');
console.log(ReactGridLayout);
// --> function ReactGridLayout(props , context) { ...

It also exports some other stuff separated into namespaces: 它还会导出其他一些分隔为名称空间的内容:

for (var f in ReactGridLayout) {
  if (ReactGridLayout.hasOwnProperty(f)) {
    console.log(f);
  }
}
// --> utils
// ...

So it does a single export and multiple exports. 因此,它执行单个出口多个出口。

I tried the Single Complex Object in Modules approach described on the Typescript site and this declaration file on GitHub but without much success. 我尝试了Typescript网站上描述的“ 模块中单个复杂对象”方法以及GitHub上的此声明文件,但没有成功。

Update 更新

Ignoring the other stuff for now my definition for ReactGridLayout looks like this: 现在忽略其他内容,我对ReactGridLayout定义如下所示:

// react-grid-layout.d.ts
declare module 'react-grid-layout' {

  import * as React from 'react';

  export default class ReactGridLayout<P,S> extends React.Component<P,S> {

    containerHeight():void;

    onWidthChange(width:number):void;

   /* more methods here ... */

}

Which compiles. 哪个编译。 However it generates Javascript like: 但是,它生成如下的Javascript:

var react_grid_layout_1 = require('react-grid-layout');

React.createElement(react_grid_layout_1.default, null, ...

When it should be: 什么时候应该是:

React.createElement(react_grid_layout_1, null, ...    

Because the react-grid-layout is exporting the ReactGridLayout class "directly" rather than as default you can't do a export default (you've seen what happens already). 因为react-grid-layout是直接“而不是default ”导出ReactGridLayout类,所以您不能export default (您已经看到了发生的情况)。 I think that you have to resolve to export = syntax, which however seems to limit you to exporting only one thing. 我认为您必须解析为export =语法,但是这似乎将您限制为仅导出一件事。 One way to solve this is to make use of TypeScript's declaration merging. 解决此问题的一种方法是利用TypeScript的声明合并。 Export a class and a namespace with the same name. 导出具有相同名称的类和名称空间。 It is more or less what react-grid-layout is doing. react-grid-layout在做什么或多或少。

main.tsx main.tsx

import * as React from 'react'
import * as ReactGridLayout from 'react-grid-layout'

var grid = new ReactGridLayout(null, null);
var responsiveGrid = new ReactGridLayout.ResponsiveReactGridLayout(null, null);

<ReactGridLayout></ReactGridLayout>
//<ReactGridLayout.ResponsiveReactGridLayout></ReactGridLayout.ResponsiveReactGridLayout>

main.js (transpiled) main.js(转译)

"use strict";
var React = require('react');
var ReactGridLayout = require('react-grid-layout');
var grid = new ReactGridLayout(null, null);
var responsiveGrid = new ReactGridLayout.ResponsiveReactGridLayout(null, null);
React.createElement(ReactGridLayout, null);
//<ReactGridLayout.ResponsiveReactGridLayout></ReactGridLayout.ResponsiveReactGridLayout>

types.d.ts types.d.ts

declare module 'react-grid-layout' {

    import * as React from 'react';

    class ReactGridLayout extends React.Component<ReactGridLayout.Props, ReactGridLayout.State> {
        // members
    }

    namespace ReactGridLayout {
        export interface State {
            activeDrag?: any; // declare LayoutItem, etc..
            // etc...
        }

        export interface Props {
            className?: string
            // etc...
        }

        export class ResponsiveReactGridLayout extends React.Component<any, any>
        {
            // etc
        }
    }

    export = ReactGridLayout;
}

BTW: You may find it easier to look at the ES6 code of react-grid-layout . 顺便说一句:您可能会发现看一下react-grid-layout的ES6代码更加容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM