添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account updateSelected ( selectedStr : string ) { const selected = selectedStr . split ( ',' ) ; this . selectedItems = _ . transform ( selected , ( items , value ) => { const found = _ . find ( this . items , { value } ) ; if ( found ) { // Error (see below) items . push ( found ) ; } , [ ] as Item [ ] ) ;

Line items.push(found) produces the following error:

Argument of type 'number | (<U>(callbackfn: (value: Item, index: number, array: Item[]) => U, thisArg?: any) => U[]...' is not assignable to parameter of type 'Item'.
  Type 'number' is not assignable to type 'Item'.

The fun thing is if I change Item['value'] type to string the error goes away.

Versions:

  • Tested with [email protected] / @2.8.3
  • @types/[email protected]
  • Mentions: @bczengel @chrootsu @stepancar @aj-r @Ailrun @e-cloud @thorn0 @jtmthf @DomiR

    [@types/lodash] Strange error in _.find [@types/lodash] _.find - type number is not assignable to type 'X' May 14, 2018

    Looks like it's matching the wrong overload. It's supposed to match this:

            find<T>(
                collection: List<T> | null | undefined,
                predicate?: ListIterateeCustom<T, boolean>,
                fromIndex?: number
            ): T|undefined;

    However it doesn't due to this error:

    Argument of type '{ value: string; }' is not assignable to parameter of type 'string | [string, any] | ListIterator<Item, boolean> | PartialDeep<Item> | undefined'.
      Type '{ value: string; }' is not assignable to type 'PartialDeep<Item>'.
        Types of property 'value' are incompatible.
          Type 'string' is not assignable to type 'PartialDeep<any> | undefined'.
    

    There are a few workarounds - adding value as any seems to fix the issue:

    const found = _.find(this.items, {value: value as any});

    or this:

        this.selectedItems = _.transform(selected, (items, value: any) => {
          const found = _.find(this.items, {value});
          if (found) {
            // Error (see below)
            items.push(found);
        }, [] as Item[]);

    It should be possible to fix this using conditional types in TS 2.8, but that would require us to drop support for all earlier versions of typescript, which we're not ready to do.