带有Lodash的TypeScript:_.map(["123","234"],_.trim)返回boolean []? [英] TypeScript with Lodash: _.map(["123", " 234 "], _.trim) returns boolean[]?

查看:111
本文介绍了带有Lodash的TypeScript:_.map(["123","234"],_.trim)返回boolean []?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按如下方式拆分的字符串数组:

  var searchValue ="600-800,123,180";var groups = searchValue.split(,");//=>["600-800","123","180"] 

(因此项目周围可能有空白),我想删除空白.我知道我可以将 Array.prototype.map String.prototype.trim 一起使用,但是我想要一个专门使用Lodash的解决方案.

我正在运行TypeScript 2.3.2和Lodash 4.17.4

奇怪的是,如果我说:

var值:string [] = _.map(['foo','bar'],String.prototype.trim);

TypeScript错误消失了,但是当 searchValue 为空,并且 groups 返回 ["] :

TypeError:String.prototype.trim调用为null或未定义

我尝试过的事情:

  var值:string [] = _.map(_.filter(groups,group => group.indexOf("-)=== -1),_.trim);var values:string [] = _.map(_.filter(groups,function(group){return group.indexOf("-)=== -1;}),_.trim);var values:string [] = _.map< string,(string ?: string,chars ?: string)=>字符串>(_.filter(groups,function(group){return group.indexOf(-")=== -1}),_.trim);var values:string [] = _.map< string,(string ?: string,chars ?: string)=>string []>(_.filter(groups,function(group){return group.indexOf(-")=== -1}),_.trim);var values:string [] = _.map< string,(string:string,chars:string)=>字符串>(_.filter(groups,function(group){return group.indexOf(-")=== -1}),_.trim);var values:string [] = _.map< string,(string:string)=>字符串>(_.filter(groups,function(group){return group.indexOf(-")=== -1}),_.trim); 

全部无济于事.我很茫然.我在这里做错什么了吗,或者这可能是Lodash/TypeScript错误?

解决方案

以下变化似乎有效:

  let值:string [] = _.map(['foo','bar'],str => _.trim(str)) 

这是为什么的原因. map 的重载很多,而您要针对此重载:

  map< T,TResult>(集合:List< T>|空|不明确的,iteratee:ListIterator< T,TResult>):TResult []; 

其中 ListIterator 定义为:

  type ListIterator< T,TResult>=(值:T,索引:数字,集合:List< T>)=>TResult; 

因此,您希望修整功能与上述 ListIterator 的定义相匹配.但是 trim 实际上定义为:

  trim(字符串?:字符串,字符?:字符串): 细绳; 

因此,当您执行以下操作时:

  let值:string [] = _.map(['foo','bar'],_.trim); 

您实际上最终遇到了另一个过载,定义为:

  map< T,TObject扩展了{}>(集合:List T | Dictionary T | NumericDictionary T|空|不明确的,iteratee?:TObject):boolean []; 

因此,当您切换到以下版本时:

  let值:string [] = _.map(['foo','bar'],str => _.trim(str)) 

您的修整函数 str =>_trim(str)符合 ListIterator ,即使它省略了最后两个参数( index collection ).

I have an array of strings which have been split like so:

var searchValue = "600-800, 123, 180";
var groups = searchValue.split(","); // => ["600-800", " 123", " 180"]

(so there is potentially whitespace around the items) and I would like to remove the whitespace. I know that I can use Array.prototype.map with String.prototype.trim, but I would like a solution specifically using Lodash.

According to the docs, I should be able to say: _.map([' foo ', ' bar '], _.trim); and it will return ['foo', 'bar'], which is a string[].

However, TypeScript is barking at me. Here's what I'm seeing in my editor.

I am running TypeScript 2.3.2 and Lodash 4.17.4

Strangely enough, if I say:

var values:string[] = _.map([' foo ', ' bar '], String.prototype.trim);

The TypeScript errors go away, but I get the following runtime error when searchValue is empty, and groups returns [""]:

TypeError: String.prototype.trim called on null or undefined

Things I have tried:

var values:string[] = _.map(_.filter(groups, group => group.indexOf("-") === -1), _.trim);
var values:string[] = _.map(_.filter(groups, function (group) { return group.indexOf("-") === -1; }), _.trim);
var values:string[] = _.map<string, (string?: string, chars?: string) => string>(_.filter(groups, function (group) { return group.indexOf("-") === -1 }), _.trim);
var values:string[] = _.map<string, (string?: string, chars?: string) => string[]>(_.filter(groups, function (group) { return group.indexOf("-") === -1 }), _.trim);
var values:string[] = _.map<string, (string: string, chars: string) => string>(_.filter(groups, function (group) { return group.indexOf("-") === -1 }), _.trim);
var values:string[] = _.map<string, (string: string) => string>(_.filter(groups, function (group) { return group.indexOf("-") === -1 }), _.trim);

All to no avail. I am at a loss. Is there something I am doing wrong here or is this possibly a Lodash/TypeScript bug?

解决方案

The following variation seems to work:

let values: string[] = _.map(['  foo  ', '  bar  '], str => _.trim(str))

Here's my guess as to why this is happening. There are many overloads for map, and you want to target this one:

map<T, TResult>(
    collection: List<T> | null | undefined,
    iteratee: ListIterator<T, TResult>
): TResult[];

where ListIterator is defined as:

type ListIterator<T, TResult> = (value: T, index: number, collection: List<T>) => TResult;

Thus you want the trimming function to match the above definition for ListIterator. But trim is actually defined as:

trim(
    string?: string,
    chars?: string
): string;

So when you do the following:

let values: string[] = _.map(['  foo  ', '  bar  '], _.trim);

You actually end up hitting a different overload, which is defined as:

    map<T, TObject extends {}>(
        collection: List<T>|Dictionary<T>|NumericDictionary<T> | null | undefined,
        iteratee?: TObject
    ): boolean[];

So when you switch to the following variation:

let values: string[] = _.map(['  foo  ', '  bar  '], str => _.trim(str))

Your trimming function str => _trim(str) conforms to ListIterator, even though it leaves out the last 2 parameters (index and collection).

这篇关于带有Lodash的TypeScript:_.map(["123","234"],_.trim)返回boolean []?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆