typescript 如何在打字稿中使用来自绝对类型化的下划线库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14585324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:47:46  来源:igfitidea点击:

How to use underscore lib from DefinitelyTyped with typescript?

typescriptdefinitelytyped

提问by Freewind

DefinitelyTypedhas provided a underscore declaration file, which defines a Listinterface, and used it heavily in the code.

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;
}

I'm trying to use the countByfunction:

我正在尝试使用该countBy功能:

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

declare var _: UnderscoreStatic;

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

When I compile the file, it throws error:

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

> 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[]'

I don't know why this error happened, since number[]fit the interface List.

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

Where is wrong, and how to fix it?

哪里错了,如何解决?

采纳答案by Fenton

You need to pass an object compatible with the Listinterface, which is an array with a length:

您需要传递一个与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;
});

In all honesty, an array technically fulfils this as it has a length property - but the above code compiles.

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

The shorthand version of this is a bit nasty:

这个的简写版本有点讨厌:

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

var list = <List><any> [1, 2, 3];

_.countBy(list, function (item) {
    return item % 2;
});

回答by Stuart Hallows

Check that the underscore TypeScript definition file matches the version of underscore you're using. The signature of countBy has changed and if the TS definition didn't match the underlying JS you'd get some unexpected behaviour.

检查下划线 TypeScript 定义文件是否与您使用的下划线版本匹配。countBy 的签名已更改,如果 TS 定义与底层 JS 不匹配,您会得到一些意外行为。

回答by VadimB

First add underscore typing:

首先添加下划线输入:

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

Then reference this file in your .ts source

然后在您的 .ts 源中引用此文件

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

Then import underscore to avoid compilation errors (be careful - underscore lib in this case should be installed as npm reference, not bower: npm install underscore --save)

然后导入下划线以避免编译错误(小心 - 在这种情况下下划线 lib 应安装为 npm 参考,而不是 bower: npm install underscore --save

import _ = require('underscore');

Then use underscore as usual using "_"global variable

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

_.isNumber(123);

回答by hd84335

Using Typescript 2.0, you can import underscore with the importstatement like below:

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

import * as _ from "underscore";

Then, call any underscore function using _.<function_name>.

然后,使用 调用任何下划线函数_.<function_name>

PS: don't forget to install underscore library with npm for example.

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