microsoft / microsoft/TypeScript
ThisType would disturb Generics' inferring type when assign an expression to a type variable
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
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:
- Remove
ReturnedDfromThisType<M & ReturnedD & CC>; - Save the file;
- Error disappeared on
this.firstname, etc; - The returned type of
instanceinferred from TypeScript is as expected and would not benever;
I also reproduced it with TypeScript playground.
We can confirm that:
ReturnedDruns well in inferring type at runtime;- The wrong type may be caused by some mechanism inside
ThisType;
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der TypeScript-3.9.2-Playground-Reproduktion und vergleiche die Inferenz mit und ohne ReturnedD in ThisType<M & ReturnedD & CC>. Verfolge, wie ThisType den bedingten Typ D extends customFunc ? ReturnType : never beeinflusst. Als erledigt gilt die Aufgabe, wenn das Beispiel keine Fehler meldet und die Instanz ReturnedD nicht mehr als never inferiert.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100