Here is a code snippet:
import _ from 'lodash';
interface Item {
value: any;
}
export class Foo {
items: Item[];
selectedItems: Item[];
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:
[email protected] / @2.8.3@types/[email protected]Mentions: @bczengel @chrootsu @stepancar @aj-r @Ailrun @e-cloud @thorn0 @jtmthf @DomiR
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.
This still happens on TS 3.5
We're using TS2.8 now, so this I think can be fixed by merging the
overloads using conditional types. I can't do that right now, but if
someone submits a PR then I'll review it.
On Tue, Jul 23, 2019, 12:25 AM Edward Chu, notifications@github.com wrote:
This still happens on TS 3.5
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/DefinitelyTyped/DefinitelyTyped/issues/25758?email_source=notifications&email_token=ABVF5M6WEG4O3AQXGINPISDQA2BZ5A5CNFSM4E7VGXA2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD2R4GFA#issuecomment-514048788,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABVF5MYCP5MR7HXSLSI4HYTQA2BZ5ANCNFSM4E7VGXAQ
.
Still an issue for me in 2020
export const enhanceStudentSolutionCriteriaGenericRule = <T extends Record>(studentSolution: StudentSolution, allRecords: Array<T>, ruleName: string, recordRefKey = 'name'): StudentSolution => {
const recordsRefs: Array<string> = findByStudentSolutionCriteriaRule(studentSolution, ruleName, [], STRATEGY_DO_NOTHING);
const records: Array<T> = [];
map(recordsRefs, (recordRef: string) => {
records.push(find(allRecords, { [recordRefKey]: recordRef })); // Replace the value (string) by the full entity (object)
});
return updateStudentSolutionCriteriaRule(studentSolution, ruleName, records);
};
Using as any fixes it.
records.push(find(allRecords, { [recordRefKey]: recordRef } as any)); // Replace the value (string) by the full entity (object)
Using:
Any update?
was this issue fixed? or is it still open?
@aj-r Could you summarize the status of this issue and what should be done for a proper fix? Thank you
Status is the same as my previous comment.
We're using TS2.8 now, so this I think can be fixed by merging the overloads using conditional types. I can't do that right now, but if someone submits a PR then I'll review it.
Most helpful comment
Looks like it's matching the wrong overload. It's supposed to match this:
However it doesn't due to this error:
There are a few workarounds - adding
value as anyseems to fix the issue:or this:
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.