microsoft / microsoft/TypeScript
Type inference lost after spreading array with `ArrayLike`
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
array inference, type inference, ArrayLike, lodash compact, _.compact
🕗 Version & Regression Information
- This changed between versions 4.9.5 and 5.0.4
⏯ Playground Link
💻 Code
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 arrayLikeInferenceFunction = <T extends unknown>(
arr: ArrayLike<T | null | undefined | false | "" | 0> | null | undefined
): T[] => {
return [];
};
const arrayLikeInference = arrayLikeInferenceFunction([
{ myKey: "123", myFunction: (value) => {} }, // value is correctly inferred as string
true && { myKey: "456", myFunction: (value) => {} },
false && { myKey: "456", myFunction: (value) => {} },
...generate(),
// type inference of myFunction is lost
true && { myKey: "456", myFunction: (value) => {} }, // value is inferred as any
false && { myKey: "456", myFunction: (value) => {} },
{ 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) => {} }, // value is correctly inferred as string
true && { myKey: "456", myFunction: (value) => {} },
false && { myKey: "456", myFunction: (value) => {} },
...generate(),
// type inference of myFunction is kept
true && { myKey: "456", myFunction: (value) => {} }, // value is correctly inferred as string
false && { myKey: "456", myFunction: (value) => {} },
{ myKey: "456", myFunction: (value) => {} },
]);
🙁 Actual behavior
The typings of the arguments of myFunction are lost after the array spread when using ArrayLike. When using Array, the type inference is kept.
🙂 Expected behavior
The arguments of myFunction should be inferred as string
Additional information about the issue
I have opened a similar issue in the DefinitelyTyped repository for the lodash typings and they suggested opening an issue in this repository. https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/69410
The issue originated from using lodash.compact() with code similar to the code below:
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) => {} }, // infers value as string
true && { myKey: "456", myFunction: (value) => {} },
false && { myKey: "456", myFunction: (value) => {} },
...generate(),
{ myKey: "123", myFunction: (value) => {} }, // infers value as any
true && { myKey: "456", myFunction: (value) => {} },
false && { myKey: "456", myFunction: (value) => {} },
]);
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked Playground reproduction using TypeScript 5.0.4 and compare the ArrayLike and Array cases around the spread of generate(). Trace the type inference behavior for the callback parameters; done means the arguments of myFunction are inferred as string after the ArrayLike spread, with the reported regression covered by a test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100