microsoft / microsoft/TypeScript

Signatures with less parameters aren't assignable to compatible targets with more when their rest param is an instantiated `NoInfer`

Aperta
#59,668 2 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: check: Type Inference Help Wanted Possible Improvement
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔎 Search Terms

NoInfer signature parameters list rest

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

https://www.typescriptlang.org/play/?ts=5.6.0-dev.20240817#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXzCgggB4BBeEADwxFWAGd44pg8IBPeNAa1RwDuqANoBdAHwAKAFDx4sAOYAueJIB0GxQxUA5HAElUiEDHLiAlPAC84+ADccWYABpZ8DWq0qy0894Dc0mB4DBjwWEYmcMDWBEQQkpJQKqjIALYARiaWNvAA3gC+zvAAjMUATOaBAPTVcvUNjfAAegD80tKgkLAIKOjYeHHE5eSUNHSMzCCs7Fy8-EJiUm6KKuqaMAra8GQ5tg5OrnIeXju+AUEhYRHGMNHlsYTDJMKpmSbFb1kwEonJ8F9stZbIVimV4JUanV6m1pEA

💻 Code
declare function call<A extends readonly unknown[]>(
  arg: (...args: NoInfer<A>) => void,
  ...args: A
): A;

// Argument of type '(a: number) => void' is not assignable to parameter of type '(...args: NoInfer<[number, number]>) => void'.
//   Types of parameters 'a' and 'args' are incompatible.
//     Type '[number, number]' is not assignable to type '[a: number]'.
//       Source has 2 element(s) but target allows only 1.(2345)
const inferred = call((a: number) => {}, 1, 2);
//               ^? function call<[number, number]>(arg: (...args: NoInfer<[number, number]>) => void, args_0: number, args_1: number): [number, number]


declare function call2<A extends readonly unknown[]>(
  arg: (...args: A) => void,
  ...args: A
): A;
const inferred2 = call2<[number, number]>((a: number) => {}, 1, 2);
//    ^? const inferred2: [number, number]
🙁 Actual behavior

The first call fails to typecheck

🙂 Expected behavior

Both of those calls use the same arguments. The first one is using NoInfer so the covariant inference can get prioritized. That prevents the instantiated signature from typechecking like the second one. Note that the first call infers the same type argument as the one that is supplied explicitly to the second call: [number, number]

Additional information about the issue

I think that this can be fixed by normalizing NoInfer<[A, B]> to [NoInfer<A>, NoInfer<B>]. This would be similar to what instantiateMappedTupleType does today at times.

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 il TypeScript Playground collegato e riproduci le due chiamate usando una tupla NoInfer istanziata. Traccia la gestione di NoInfer da parte del checker nell’assegnabilità di tuple e parametri rest, in particolare il comportamento di normalizzazione suggerito nell’issue. Il lavoro è completato quando sia la chiamata inferita sia quella tipizzata esplicitamente superano il controllo dei tipi con la tupla prevista [number, number].

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
42/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.