microsoft / microsoft/TypeScript

ThisType would disturb Generics' inferring type when assign an expression to a type variable

Aperta
#39,848 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Needs Investigation
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

TypeScript Version: 3.9.2, and it does exist in 'Nightly' version, too.

Search Terms: ThisType, Generics, Inferring problem

Code

type customFunc = (...args: any) => any

declare function SimpleVue<
  D,
  C extends { [K in string]: customFunc },
  M = { [K in string]: customFunc },
  CC = { [K in keyof C]: ReturnType<C[K]> },
  ReturnedD = D extends customFunc ? ReturnType<D> : never,
>(option: {
  data: D,
  methods: M,
  computed: C
} & ThisType<M & ReturnedD & CC>): ReturnedD

const instance = SimpleVue({
  data() {
    return {
      firstname: 'Type',
      lastname: 'Challenges',
      amount: 10,
    }
  },
  computed: {
    fullname() {
      return this.firstname + ' ' + this.lastname
    }
  },
  methods: {
    hi() {
      alert(this.fullname.toLowerCase())
    }
  }
});

Expected behavior:

We expect ReturnedD = D extends customFunc ? ReturnType<D> : never infers ReturnedD to ReturnType<D> at runtime since D fits the shape of customFunc:

() => {
    firstname: string;
    lastname: string;
    amount: number;
}

And the code should show no error in TypeScript environment.

Actual behavior:

The actual behavior is that ReturnedD is inferred to never type since D extends customFunc here is false at runtime.

You can also check it with the returned type of instance as well.

Playground Link:

Check out the link here.

Personal speculation:

If you look at the demo above, you can do following steps to remove the error hint and confirm ReturnedD's correction:

  1. Remove ReturnedD from ThisType<M & ReturnedD & CC>;
  2. Save the file;
  3. Error disappeared on this.firstname, etc;
  4. The returned type of instance inferred from TypeScript is as expected and would not be never;

I also reproduced it with TypeScript playground.

We can confirm that:

  1. ReturnedD runs well in inferring type at runtime;
  2. The wrong type may be caused by some mechanism inside ThisType;

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 la riproduzione nel playground di TypeScript 3.9.2 e confronta l’inferenza con e senza ReturnedD in ThisType<M & ReturnedD & CC>. Traccia il modo in cui ThisType influisce sul tipo condizionale D extends customFunc ? ReturnType : never. Il lavoro è completato quando l’esempio non segnala errori e l’istanza non inferisce più ReturnedD come never.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.