microsoft / microsoft/TypeScript

partial function with generic lost the type constraint

Aperta
#57,072 1 commento 0 reazioni 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

generic
partial function with generic lose the type constraint

🕗 Version & Regression Information
  • This is a crash
  • This changed between versions nightly__ and 4.x___
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about ___ generic, lose type______
  • I was unable to test this on prior versions because the version lower than 4.0 do not support tailargs___
⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.4.0-dev.20240107#code/PTAEFsEsDtPBDANqATgUwA4oPYBMCuAxmgFAkAuAnhmqAIIoqgC8qa8u20ilo80lANoBdMgDN80QuUhdQGeChlIAwkkQAeACqg0AD3JpouAM71GAGlABVXQaOnzKKwCUAfAAoSoUGIBcoB4AdCGKAOYmAYIhQVpWMdbCAJQsbqAuFt6gMQAW7LgMEQFaJCkA3lno5Pgo0IEx5PCQiIWRNinMaWLBIXkcrfEhjc2tSSQAvmSEXCbkEPCEOTBozBU+WJDEAQDMAAyZPvBhaH4AjLsT4pLSsnXQ+OAqOfzH2naGxmYuaNMouBqzFAwMJWe7gABGaBQbk8oGw4IAVn44qAANZoSgBdGUbBiUAo3BoRCNAJgyEoJJrHw+Ko1OrwhGCbHCADUhOJ8EuEikMjkYKeLzQpze+g+jm+v3+gOBoIe5JhHjRGKxGNx+KsDORFnZJNAZKhlKy1NptThiKZGNZOs5kxI02gs1AhkdrH5z2gxw8CEWy1AVgA5Ec0P6rOcxiQQKAAALkEwAWn0NGkCcY2BQdpmc2d5FOLD1DwFHrQXoWS2gaAsgZD5z94YzDrm3rLaAAcgX3cdWAolJBVOoPG7BVYm8tw5GTDlsPhELhQJBQCY0LR4LpU0w8uhQAB+euOwynVgj8ttx4d4tViwAJl2YyAA

💻 Code
// minimal reproduce

type Arr = readonly any[]

function partialCall<T extends Arr, U extends Arr, R>(
  f: (...args: [...T, ...U]) => R,
  ...headArgs: T
) {
  return (...tailArgs: U) => f(...headArgs, ...tailArgs)
}

const machine={
  price: 30,
  age:10
}

function numChange<T extends Record<string, number>>( obj:T, key: keyof T, delta: number){
     return obj[key]+delta
}

const test = numChange(machine , 'age', 10)

// @ts-expect-error
const test1 = numChange(machine,'a',10 ,)


const machineNumChange= partialCall(numChange, machine)

// should i see a error here ?
const te1= machineNumChange('a',20)
🙁 Actual behavior

I'm using a partial function with passing another generic function , the returned function loses the type restrictions on its parameters.

🙂 Expected behavior

the returned function have the type restrictions on its parameters

Additional information about the issue

No response

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 la riproduzione minima fornita, concentrandoti sull'interazione tra generic partialCall e numChange. Conferma il tipo inferito di machineNumChange e confrontalo con il rifiuto previsto della chiave 'a'; il lavoro è completato quando la funzione restituita preserva il vincolo sulla chiave di numChange.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.