microsoft / microsoft/TypeScript
Reverse mapped types don't use their constraint types when no candidates are present unlike the regular type parameters
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔎 Search Terms
reverse mapped constraint inference candidates
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
💻 Code
interface ParameterizedObject {
type: string;
params?: Record<string, unknown>;
}
declare function setup<
TContext,
TGuards extends Record<string, ParameterizedObject["params"] | undefined>,
>(_: {
types: {
context: TContext;
};
guards: {
[K in keyof TGuards]: (context: TContext, params: TGuards[K]) => void;
};
}): TGuards;
const result = setup({
types: {
context: {
count: 100,
},
},
guards: {
checkFoo: (_, { foo }: { foo: string }) => foo === "foo",
alwaysTrue: (_) => true,
},
});
result;
// ^?
🙁 Actual behavior
Type '(_: { count: number; }, { foo }: { foo: string; }) => boolean' is not assignable to type '(context: { count: number; }, params: Record<string, unknown> | undefined) => void'.
Types of parameters '__1' and 'params' are incompatible.
Type 'Record<string, unknown> | undefined' is not assignable to type '{ foo: string; }'.
Type 'undefined' is not assignable to type '{ foo: string; }'.(2322)
🙂 Expected behavior
I'd expect the inference to succeed here
Additional information about the issue
TGuards is inferred as { checkFoo: { foo: string; }; alwaysTrue: unknown; } and thus rejected by the constraint check in getInferredType. alwaysTrue is inferred as unknown because inferReverseMappedType has such a return:
return getTypeFromInference(inference) || unknownType
And getTypeFromInference doesn't attempt to read the type parameter's constraint at all. It simply handles .candidates and .contraCandidates and that's it.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la riproduzione in Playground e segui i percorsi di controllo dei tipi indicati nel report: getInferredType, inferReverseMappedType e getTypeFromInference. Analizza come vengono gestiti i tipi vincolo quando l'inferenza reverse-mapped non ha candidati; il lavoro è completato quando la chiamata di configurazione fornita viene inferita correttamente senza l'errore di vincolo mostrato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 30/100