【问题标题】:How to use underscore lib from DefinitelyTyped with typescript?如何使用来自DefinitelyTyped的下划线库和打字稿?
【发布时间】:2013-01-13 03:59:20
【问题描述】:

DefinitelyTyped提供了下划线声明文件,定义了List接口,并在代码中大量使用。

// Common interface between Arrays and jQuery objects
interface List {
    [index: number]: any;
    length: number;
}

interface UnderscoreStatic {
    sortBy(list: List, iterator?: any, context?: any): any;
    groupBy(list: List, iterator: any): any;
    countBy(list: List, iterator: any): any;
}

我正在尝试使用countBy 函数:

// <reference path="../DefinitelyTyped/underscore/underscore.d.ts" />

declare var _: UnderscoreStatic;

_.countBy([1,2,3], function(item) {
    return item%2;
});

当我编译文件时,它会抛出错误:

> tsc commons.ts

> E:/commons.ts(5,0): Supplied parameters do not match any signature of call target:
    Could not apply type 'List' to argument 1, which is of type 'number[]'

我不知道为什么会出现这个错误,因为number[]适合接口List

哪里出了问题,如何解决?

【问题讨论】:

  • 2016 年更新:import * as _ from "underscore";

标签: typescript underscore.js definitelytyped


【解决方案1】:

2021 年更新

将以下依赖添加到package.json

  "dependencies": {
    "underscore": "^1.13.1"
  },
  "devDependencies": {
    "@types/underscore": "^1.11.3"
  }

然后运行:

$ npm install

在您的 TypeScript 文件中,使用以下命令导入 underscore

import * as _ from 'underscore';

【讨论】:

    【解决方案2】:

    使用 Typescript 2.0,您可以使用 import 语句导入下划线,如下所示:

    import * as _ from "underscore";
    

    然后,使用_.&lt;function_name&gt; 调用任何下划线函数。

    PS:不要忘记使用 npm 安装下划线库。

    【讨论】:

      【解决方案3】:

      先加下划线输入:

      npm install typings --global
      typings install dt~jasmine --save --global
      

      然后在你的 .ts 源中引用这个文件

      /// <reference path="../../../typings/underscore.d.ts" />
      

      然后导入下划线以避免编译错误(注意 - 在这种情况下下划线库应安装为 npm 引用,而不是 bower:npm install underscore --save

      import _ = require('underscore');
      

      然后像往常一样使用下划线使用 "_" 全局变量

      _.isNumber(123);
      

      【讨论】:

        【解决方案4】:

        检查下划线 TypeScript 定义文件是否与您使用的下划线版本匹配。 countBy 的签名发生了变化,如果 TS 定义与底层 JS 不匹配,你会得到一些意想不到的行为。

        【讨论】:

          【解决方案5】:

          你需要传递一个与List接口兼容的对象,它是一个有长度的数组:

          /// <reference path="underscore.d.ts" />
          
          var list: List;
          list[0] = 1;
          list[1] = 2;
          list[2] = 3;
          list.length = 3;
          
          _.countBy(list, function (item) {
              return item % 2;
          });
          

          老实说,数组在技术上实现了这一点,因为它具有长度属性 - 但上面的代码可以编译。

          这个的简写版本有点恶心:

          /// <reference path="underscore.d.ts" />
          
          var list = <List><any> [1, 2, 3];
          
          _.countBy(list, function (item) {
              return item % 2;
          });
          

          【讨论】:

          • 我无法想象每次使用数组时都必须将length 添加到数组中,以使其能够编译。
          • 你不能也只是投射数组:_.countBy((&lt;List&gt;[1,2,3]) ... 等吗? (手头没有下划线库,所以无法测试)。
          • 您得到“无法将数字 [] 转换为列表”。一种选择是接受number[] 将是更好的输入underscore.d.ts,因为我们知道它将有一个索引器和一个长度。你可以摆脱真正可怕的:var list: List = &lt;List&gt;&lt;any&gt; [1, 2, 3];
          • @Freewind 我添加了一个有点讨厌的速记版本,但意味着您不必使用扩展版本。请记住,这很可能是 .d.ts 定义中的错误 - 普通下划线用户可能能够确认,在这种情况下,我很乐意提交更新。
          • 更新 - 我已经提交了一个关于“绝对类型”的拉取请求,以删除列表接口以支持使用 any[] 类型,这将允许正常使用下划线。
          猜你喜欢
          • 2020-09-05
          • 2018-05-24
          • 2021-06-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-10-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多