[@types/lodash] _.compact loses type inference on objects that follow after array spread #69410
Replies: 4 comments 1 reply
-
Thanks for the discussion about "lodash", some useful links for everyone: Pinging the DT module owners: @bczengel, @chrootsu, @aj-r, @e-cloud, @thorn0, @jtmthf, @DomiR, @WilliamChelman. |
Beta Was this translation helpful? Give feedback.
-
I'm not really sure what's going on here, since the type of compact<T>(array: ArrayLike<T | null | undefined | false | "" | 0> | null | undefined): T[]; The main difference I see is that |
Beta Was this translation helpful? Give feedback.
-
We could add an Array overload before the ArrayLike overload, which should
be unlikely to break anything. I suspect this affects more than just
compact though... I'm not sure if it's worth doing this for all array
methods too?
…On Mon, Apr 22, 2024, 1:16 p.m. KarelVerschraegen ***@***.***> wrote:
It looks like it might indeed be a TypeScript issue. Given the following
code (
https://codesandbox.io/p/sandbox/romantic-pine-9lx3g2?file=%2Fsrc%2Findex.ts%3A13%2C16
):
- If we use _.compact to infer the typings, we *lose* inference after
the spread
- If we use the built-in ArrayLike TypeScript interface, we *lose*
inference after the spread
- If we use the built-in Array TypeScript interface, we *keep*
inference after the spread
import _ from "lodash";
type MyType<T extends unknown> = {
myKey: T;
myFunction: (arg1: T) => void;
};
const generate = (): Array<MyType<string>> => {
return Array.from({ length: 5 }).map<MyType<string>>((index) => {
return {
myKey: `${index}`,
myFunction: (arg1) => {
return;
},
};
});
};
const lodashInferred = _.compact([
{ myKey: "123", myFunction: (value) => {} },
true && { myKey: "456", myFunction: (value) => {} },
false && { myKey: "456", myFunction: (value) => {} },
...generate(),
// type inference of myFunction is lost
{ myKey: "123", myFunction: (value) => {} },
true && { myKey: "456", myFunction: (value) => {} },
false && { myKey: "456", myFunction: (value) => {} },
]);
const arrayLikeInferenceFunction = <T extends unknown>(
arr: ArrayLike<T | null | undefined | false | "" | 0> | null | undefined
): T[] => {
return [];
};
const arrayLikeInferrence = arrayLikeInferenceFunction([
{ myKey: "123", myFunction: (value) => {} },
true && { myKey: "456", myFunction: (value) => {} },
false && { myKey: "456", myFunction: (value) => {} },
...generate(),
// type inference of myFunction is lost
{ myKey: "456", myFunction: (value) => {} },
true && { myKey: "456", myFunction: (value) => {} },
false && { myKey: "456", myFunction: (value) => {} },
]);
const arrayInferenceFunction = <T extends unknown>(
arr: Array<T | null | undefined | false | "" | 0> | null | undefined
): T[] => {
return [];
};
const arrayInference = arrayInferenceFunction([
{ myKey: "123", myFunction: (value) => {} },
true && { myKey: "456", myFunction: (value) => {} },
false && { myKey: "456", myFunction: (value) => {} },
...generate(),
// type inference of myFunction is kept
{ myKey: "456", myFunction: (value) => {} },
true && { myKey: "456", myFunction: (value) => {} },
false && { myKey: "456", myFunction: (value) => {} },
]);
I'll open an issue in the TypeScript repository and link back to this one.
Should we update the typings of _.compact to Array instead of ArrayLike
or might that break other use-cases? I can open a PR if necessary.
Thank you for your help already!
—
Reply to this email directly, view it on GitHub
<#69410 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABVF5M77JTM4K5ULNGGIQXTY6VAYXAVCNFSM6AAAAABGSNESZ6VHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TCOJRG43DI>
.
You are receiving this because you were mentioned.Message ID:
<DefinitelyTyped/DefinitelyTyped/repo-discussions/69410/comments/9191764@
github.com>
|
Beta Was this translation helpful? Give feedback.
-
I have opened an issue in the TypeScript repository for those interested microsoft/TypeScript#58286 |
Beta Was this translation helpful? Give feedback.
-
Given the following CodeSandbox: https://codesandbox.io/p/sandbox/romantic-pine-9lx3g2?file=%252Fsrc%252Findex.ts, the _.compact function loses type inference on all objects that follow after an array spread.
Looking at the first object, we correctly see value inferred as string
Looking at the second object, we see that
value
loses the string inference and is typed asany
Beta Was this translation helpful? Give feedback.
All reactions