microsoft / microsoft/TypeScript

Function expressions widen to `{}` in auto arrays

Aperta
#57,627 2 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔎 Search Terms

function expression widen widening auto array any empty objecr

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.5.0-dev.20240304#code/MYewdgzgLgBAhgJwQRhgXhgbQLoG4BQ+iKAdAA4CuEAFgBQBmFYwUAluDLQJQwDe+MGAgCmUCgjAwARFIIBfLgSJJkBAPRqYAPQD8hUJFjEATOix5CJ8lTrd0APj4Cho8ZJnzFlpMfWbd+EA

💻 Code
const arr1 = [];

arr1.push(function () {
  return "";
});

arr1;
// ^? const arr1: {}[]

const arr2 = [];

arr2.push(() => {
  return "";
});

arr2;
// ^? const arr2: (() => string)[]
🙁 Actual behavior

arr1's type after this .push is {}[]

🙂 Expected behavior

I'd expect the type at both of the checked locations to be the same ((() => string)[])

Additional information about the issue

Likely the difference comes from the fact that the function expression is context-sensitive - maybe this is just a design limitation. However, the same doesn't happen in JS with expando objects (TS playground):

const obj = {}

obj.foo = function () {
  return ""
}

const result = obj.foo
//    ^? const result: () => string

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con l'esempio collegato di TypeScript Playground e confronta i tipi inferiti di arr1 e arr2 dopo le rispettive chiamate a push. Usa l'esempio aggiuntivo di JavaScript expando-object come punto di confronto; il lavoro è completato quando i casi function-expression e arrow-function hanno tipi degli elementi degli array inferiti coerenti, con una copertura di regressione per il comportamento segnalato.

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
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.