Definitelytyped: [@types/lodash]: pickBy has incorrect signature when using it with Dictionary

Created on 7 Mar 2018  路  6Comments  路  Source: DefinitelyTyped/DefinitelyTyped

  • [x] I tried using the @types/lodash package and had problems. (version 4.14.104)
  • [x] I tried using the latest stable version of tsc. https://www.npmjs.com/package/typescript
  • [x] I have a question that is inappropriate for StackOverflow. (Please ask any appropriate questions there).

After upgrading to 4.14.104, the error appears when trying to filter a Dictionary<number> with _.pickBy and reassign to the same variable:

error TS2322: Type 'Partial<Dictionary<number>>' is not assignable to type 'Dictionary<number>'.
  Index signatures are incompatible.
    Type 'number | undefined' is not assignable to type 'number'.
      Type 'undefined' is not assignable to type 'number'.

The code is similar to this:

let mapping: Dictionary<number> = {key1: 5, key2: -3}
mapping = _.pickBy(mapping, v => v > 0)

Ideally, the result of _.pickBy has to be also a Dictionary<T> in this case.
After all, the new object is created which doesn't have some of the keys.
It is not the same as the object that has some of the values changed to undefined.

@andy-ms @bczengel @chrootsu @stepancar @aj-r @Ailrun @e-cloud @thorn0 @jtmthf @DomiR

All 6 comments

For now there's a workaround: use Dictionary<number | undefined> everywhere.

let mapping: Dictionary<number | undefined> = {key1: 5, key2: -3}
mapping = _.pickBy(mapping, v => v > 0)

@vvlevykin I think this is fixed since @types/[email protected]. Can you try with the latest version, and close this issue if it works?

Appears to be working, thanks!

@aj-r I'm still seeing this issue in @types/[email protected] with Dictionary<number> as well as dictionaries of types I've defined myself. I tried typescript 2.7.2 and 2.9.2. Is this expected?

Today, I also updated my TypeScript version and other package versions (including @types), and I'm seeing this error :(

I'm looking at the history of pickBy, and it looks like it hasn't changed since February. That said, I'm still reproducing the issue. It looks like this was fixed for pick, but not for pickBy.

I'll see if there's any way to fix pickBy, but there may not be a good solution (without using conditional types, which we can't use because we still need to support TS 2.2).

Was this page helpful?
0 / 5 - 0 ratings