microsoft / microsoft/TypeScript

Unexpected widening of template literal string type when the function's type is referenced by the constraint of a additional uninvolved type parameter

Aperta
#56,724 0 commenti 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 constraints", "Infer strict return type"

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Strict function types
⏯ Playground Link

No response

💻 Code

First example:

type Primitives = number | string | boolean | bigint | symbol | undefined | null | void;

declare function call<
  R extends Primitives,
  O extends { a: (...args: any[]) => R },
  Args extends { a: Parameters<O['a']> }
>(o: O, args: Args): ReturnType<O['a']>;

const check = call(
  {
    a(a: string) {
      return `${a}a`;
    }
  },
  { a: ['a'] }
)

Second example:

type Primitives = number | string | boolean | bigint | symbol | undefined | null | void;

declare function call<
  R extends Primitives,
  O extends { a: (...args: any[]) => R },
>(o: O, args: { a: Parameters<O['a']> }): ReturnType<O['a']>;

const check = call(
  {
    a(a: string) {
      return `${a}a`;
    }
  },
  { a: ['a'] }
)
🙁 Actual behavior

I'm trying to write a function that retrieves the function's strict return type (which is a property of an object) and takes a second argument as an object with the same properties, but with arguments for these functions.

There is a problem with how TypeScript infer the return types.

In the first example check will have string type. But in the second example check will have ${string}a type as expected.

I have noticed that problem occur when I try to write generic for args argument of call function.

This looks like a bug, since generic for args argument has no effect on other types.

🙂 Expected behavior

In both examples the return type must be ${string}a.

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 riproducendo le due dichiarazioni di chiamata generiche e verificando il tipo inferito di check in ciascun esempio. Esamina i percorsi di inferenza dei vincoli generici e del tipo restituito di TypeScript, quindi aggiungi un test di regressione che dimostri che il primo esempio conserva il tipo literal di template ${string}a, proprio come il secondo.

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.