代码之家  ›  专栏  ›  技术社区  ›  Andrew

类型脚本索引。d、 npm包中深度嵌套函数的ts声明

  •  1
  • Andrew  · 技术社区  · 7 年前

    不幸地 @types/rickshaw 似乎是空的,所以我必须制作自己的声明文件。这是我当前的一个:

    declare module 'rickshaw' {
        export class Graph {
            constructor(obj: any);
            render: any;
        }
    }
    

    这让我可以使用这些方法 render 并创建一个新实例。但现在我想声明一个来自Rickhaw的深度嵌套方法。我当前的声明不允许我使用它,错误如下:

    Rickshaw.Graph.Axis.Time({graph: newGraph})
    (25,41): Property 'Axis' does not exist on type 'typeof Graph'.
    

    我检查了该方法的源代码,结果如下

    Rickshaw.namespace('Rickshaw.Graph.Axis.Time');
    
    Rickshaw.Graph.Axis.Time = function(args) {
     //...
    }
    

    如何申报 Rickshaw.Graph.Axis.Time ?

    编辑:这是我的新声明文件。我现在遇到了 'new' expression, whose target lacks a constructor signature implicitly has an any type

    interface TimeArgs {
        graph: typeof Graph;
        ticksTreatment: string;
    }
    
    interface AxisInterface {
        Time: (args: TimeArgs) => void;
    }
    
    export class Graph {
        constructor(obj: any);
        render: any;
        static Axis: AxisInterface;
    }
    

    我试图通过陈述 tyepof Graph ,因为 graph 中的参数 Time 是的实例 new Rickshaw.Graph 对象,但我仍然得到错误。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Andrew    7 年前

    我不熟悉 rickshaw 但你已经添加了 Graph ,现在可以添加 Axis 在…内 图表 然后添加 Time 在…内 :

    declare module 'rickshaw' {
        interface TimeArgs {
            graph: any;
        }
        interface AxisInterface {
            Time: (args: TimeArgs) => void;
        }
        export class Graph {
            constructor(obj: any);
            render: any;
            static Axis: AxisInterface;
        }
    }
    

    并添加您认为合适的任何其他类型/接口。