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

正在写入类型定义文件

  •  0
  • SecretIndividual  · 技术社区  · 7 年前

    我正在学习一门似乎过时的课程(至少在我看来是这样)。我需要为Load编写一个类型定义文件(自己实现)(这是为了说明如何编写类型定义文件)。

    给出的代码是:

    declare module "lodash" {
    
      declare interface FirstFunction {
        (data: any[]): any;
      }
    
      declare interface Lodash {
    
        first: FirstFunction;
    
      }
    
      export const _: Lodash;
    
    }
    

    然而,这似乎不再起作用,因为它抛出了错误:“A”声明修饰符不能在已经存在的上下文中使用。通过移除嵌套声明来修复如下内容:

    declare module "lodash" {
    
      interface FirstFunction {
        (data: any[]): any;
      }
    
      interface Lodash {
    
        first: FirstFunction;
    
      }
    
      export const _: Lodash;
    
    }
    

    如果我现在尝试使用它(调用第一个函数),编译器会抛出一个错误:“Type Type导入(“LoAuSH”)上不存在错误TS339:属性‘第一’。

    导入lodash的.ts文件是:

    import * as _ from 'lodash';
    
    const colors = ['red', 'green', 'blue'];
    
    const firstColor = _.first(colors);
    
    console.log(firstColor);
    

    在这一点上,我开始看打字手册,并阅读他们做这件事。使用他们在示例中的方法似乎可以修复所有错误。这意味着编写类型定义文件A的工作如下:

    declare module "lodash" {
    
      export function first(data: any[]): any;
    
    }
    

    我现在的问题是,在接口中是否不再可能描述函数的输入和输出,所以它的签名是可重用的?

    0 回复  |  直到 7 年前
    推荐文章