TypeScript with Lodash: _.map (["123", "234"], _.trim) returns boolean []?

I have an array of strings that were broken like this:

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

(therefore there are potentially spaces around the elements) and I would like to remove the spaces. I know what I can use Array.prototype.mapwith String.prototype.trim, but I would like the solution to specifically use Lodash.

According to the documents , I can say: _.map([' foo ', ' bar '], _.trim);and he will return ['foo', 'bar'], which is string[].

However, TypeScript barks at me. This is what I see in my editor.

enter image description here enter image description here

I am running TypeScript 2.3.2 and Lodash 4.17.4

Oddly enough, if I say:

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

TypeScript errors go away, but I get the following runtime error when searchValueempty and groupsreturns [""]:

TypeError: String.prototype.trim null undefined

, :

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

. . -, Lodash/ TypeScript?

+4
2

, :

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

, . map, :

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

ListIterator :

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

, , ListIterator. trim :

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

, :

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

, :

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

, :

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

str => _trim(str) ListIterator, 2 (index collection).

+3

, _.trim :

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

lodash ListIterator, , , ().

, _.map, , ( " true , , else false." )

.

+3

Source: https://habr.com/ru/post/1678919/


All Articles