microsoft / microsoft/TypeScript
Issue with union type
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.7.5
Expected behavior:
Type of parameter qm in method Service.getItems should be union type assignable QueryModel<KeyToEntityMap<E>>
Actual behavior:
Argument of type 'QueryModel<KeyToEntityMap<E>>' is not assignable to parameter of type 'QueryModel<{ x: string; }> & QueryModel<{ y: string; }>'.
Type 'QueryModel<KeyToEntityMap<E>>' is not assignable to type 'QueryModel<{ x: string; }>'.
Type '{ x: string; }' is not assignable to type 'KeyToEntityMap<E>'.
Code
type Key = "a" | "b";
type KeyToEntityMap<E extends Key> =
E extends "a" ? { x: string; } :
E extends "b" ? { y: string; } :
never;
class QueryModel<T>
{
filters: [keyof T, string][] = [];
}
class Service<E extends Key>
{
getItems(qm: QueryModel<KeyToEntityMap<E>>): KeyToEntityMap<E>[]
{
return [];
}
}
type IServiceFactory = { [E in Key]: Service<E> };
class ServicesFactory implements IServiceFactory
{
a: Service<"a"> = new Service<"a">();
b: Service<"b"> = new Service<"b">();
}
const sf: IServiceFactory = new ServicesFactory();
class Same<E extends Key>
{
fail(e: E)
{
let queryModel = new QueryModel<KeyToEntityMap<E>>();
//Here getItems parameter gets wrong intersection type QueryModel<{ x: string; }> & QueryModel<{ y: string; }>
let items: KeyToEntityMap<E> = sf[e].getItems(queryModel);
}
}
Output
"use strict";
class QueryModel {
constructor() {
this.filters = [];
}
}
class Service {
getItems(qm) {
return [];
}
}
class ServicesFactory {
constructor() {
this.a = new Service();
this.b = new Service();
}
}
const sf = new ServicesFactory();
class Same {
fail(e) {
let items = sf[e].getItems(new QueryModel());
}
}
Compiler Options
{
"compilerOptions": {
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"useDefineForClassFields": false,
"alwaysStrict": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"downlevelIteration": false,
"noEmitHelpers": false,
"noLib": false,
"noStrictGenericChecks": false,
"noUnusedLocals": false,
"noUnusedParameters": false,
"esModuleInterop": true,
"preserveConstEnums": false,
"removeComments": false,
"skipLibCheck": false,
"checkJs": false,
"allowJs": false,
"declaration": true,
"experimentalDecorators": false,
"emitDecoratorMetadata": false,
"target": "ES2017",
"module": "ESNext"
}
}
Playground Link: Provided
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 dem bereitgestellten Playground-Link und der minimalen TypeScript-Reproduktion unter den angegebenen Compileroptionen. Verfolge, wie der indizierte Service-Lookup und der generische bedingte Typ geprüft werden, und füge anschließend einen Regressionstest für den gemeldeten Assignability-Fehler hinzu. Die Aufgabe ist erledigt, wenn das Beispiel ohne die falsche Diagnose zum Intersection-Typ typgeprüft wird und dabei der gezeigte Rückgabetyp erhalten bleibt.
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