mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-19 01:33:15 -05:00
Instantiate generic conditional infer source types in the context of the target conditional (#31545)
* Instantiate generic conditional infer source types in the context of the target conditional * Add test case from #26627
This commit is contained in:
@@ -13527,9 +13527,19 @@ namespace ts {
|
||||
// Two conditional types 'T1 extends U1 ? X1 : Y1' and 'T2 extends U2 ? X2 : Y2' are related if
|
||||
// one of T1 and T2 is related to the other, U1 and U2 are identical types, X1 is related to X2,
|
||||
// and Y1 is related to Y2.
|
||||
if (isTypeIdenticalTo((<ConditionalType>source).extendsType, (<ConditionalType>target).extendsType) &&
|
||||
const sourceParams = (source as ConditionalType).root.inferTypeParameters;
|
||||
let sourceExtends = (<ConditionalType>source).extendsType;
|
||||
let mapper: TypeMapper | undefined;
|
||||
if (sourceParams) {
|
||||
// If the source has infer type parameters, we instantiate them in the context of the target
|
||||
const ctx = createInferenceContext(sourceParams, /*signature*/ undefined, InferenceFlags.None, isRelatedTo);
|
||||
inferTypes(ctx.inferences, (<ConditionalType>target).extendsType, sourceExtends, InferencePriority.NoConstraints | InferencePriority.AlwaysStrict);
|
||||
sourceExtends = instantiateType(sourceExtends, ctx.mapper);
|
||||
mapper = ctx.mapper;
|
||||
}
|
||||
if (isTypeIdenticalTo(sourceExtends, (<ConditionalType>target).extendsType) &&
|
||||
(isRelatedTo((<ConditionalType>source).checkType, (<ConditionalType>target).checkType) || isRelatedTo((<ConditionalType>target).checkType, (<ConditionalType>source).checkType))) {
|
||||
if (result = isRelatedTo(getTrueTypeFromConditionalType(<ConditionalType>source), getTrueTypeFromConditionalType(<ConditionalType>target), reportErrors)) {
|
||||
if (result = isRelatedTo(instantiateType(getTrueTypeFromConditionalType(<ConditionalType>source), mapper), getTrueTypeFromConditionalType(<ConditionalType>target), reportErrors)) {
|
||||
result &= isRelatedTo(getFalseTypeFromConditionalType(<ConditionalType>source), getFalseTypeFromConditionalType(<ConditionalType>target), reportErrors);
|
||||
}
|
||||
if (result) {
|
||||
|
||||
Reference in New Issue
Block a user