microsoft / microsoft/TypeScript
Reverse mapped types don't use their constraint types when no candidates are present unlike the regular type parameters
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Playground repro and trace the type-checking paths named in the report: getInferredType, inferReverseMappedType, and getTypeFromInference. Investigate how constraint types are handled when reverse-mapped inference has no candidates; done means the provided setup call infers successfully without the shown constraint error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100