microsoft / microsoft/TypeScript
Pick<T, Exclude<keyof T, K>> & Pick<T, Extract<keyof T, K>> should be assignable to T
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.2.1
Search Terms:
3.2.1
extends
intersection generic
Code
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
type Func<T> = (arg: T) => null
type Context = 'Context';
export function withRouteContextPropConsumer<
T extends { routeContext: Context }
>(
funcToWrap: Func<T>,
): Func<Omit<T, "routeContext">> {
return (args: Omit<T, "routeContext">) => {
const routeContext: Context = 'Context';
return funcToWrap({ ...args, routeContext });
};
}
Expected behavior:
Code compiles without errors
Actual behavior:
Argument of type '{ routeContext: "Context"; }' is not assignable to parameter of type 'T'.
After upgrading from 3.0.3 to 3.2.1, it seems that tsc has (at least partially) lost the ability to reason about constrained generics.
In the example above (one of our React context helper functions, modified to remove the React dependency), the function is parameterised over a constrained generic:
T extends { routeContext: Context }
But a few lines later, the compiler complains that the generic T may not have a routeContext attribute. T must have a routeContext attribute however, because of the constraint. Perhaps the Omit helper is confusing things?
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
Reproduziere den TypeScript-3.2.1-Fehler mit dem bereitgestellten Playground-Beispiel und tsc. Verfolge, wie eingeschränkte Generics, Omit über Pick/Exclude und Object Spread geprüft werden; abgeschlossen ist die Aufgabe, wenn das Beispiel ohne Fehler kompiliert und das Verhalten durch Regressionstests abgedeckt ist.
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
- Klar beschrieben
- Anfängerfreundlichkeit
- 38/100