microsoft / microsoft/TypeScript
Type inference lost after spreading array with `ArrayLike`
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔎 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) => {} },
]);
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la riproduzione collegata in Playground usando TypeScript 5.0.4 e confronta i casi ArrayLike e Array relativi allo spread di generate(). Traccia il comportamento dell’inferenza dei tipi per i parametri del callback; il lavoro è concluso quando gli argomenti di myFunction vengono inferiti come string dopo lo spread di ArrayLike, con la regressione segnalata coperta da un test.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100