Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defer simplification of conditionals on deferred type references #37423

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 99 additions & 31 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9853,7 +9853,7 @@ namespace ts {
}

function getPropertiesOfType(type: Type): Symbol[] {
type = getApparentType(getReducedType(type));
type = getApparentType(type);
return type.flags & TypeFlags.UnionOrIntersection ?
getPropertiesOfUnionOrIntersectionType(<UnionType>type) :
getPropertiesOfObjectType(type);
Expand Down Expand Up @@ -10195,6 +10195,7 @@ namespace ts {
* type itself.
*/
function getApparentType(type: Type): Type {
type = getReducedType(type);
const t = type.flags & TypeFlags.Instantiable ? getBaseConstraintOfType(type) || unknownType : type;
return getObjectFlags(t) & ObjectFlags.Mapped ? getApparentTypeOfMappedType(<MappedType>t) :
t.flags & TypeFlags.Intersection ? getApparentTypeOfIntersectionType(<IntersectionType>t) :
Expand Down Expand Up @@ -10352,15 +10353,21 @@ namespace ts {
* no constituent property has type 'never', but the intersection of the constituent property types is 'never'.
*/
function getReducedType(type: Type): Type {
if (type.flags & TypeFlags.Union && (<UnionType>type).objectFlags & ObjectFlags.ContainsIntersections) {
if (type.flags & TypeFlags.Union && (<UnionType>type).objectFlags & ObjectFlags.ContainsReducibles) {
return (<UnionType>type).resolvedReducedType || ((<UnionType>type).resolvedReducedType = getReducedUnionType(<UnionType>type));
}
else if (type.flags & TypeFlags.Intersection) {
if (!((<IntersectionType>type).objectFlags & ObjectFlags.IsNeverIntersectionComputed)) {
(<IntersectionType>type).objectFlags |= ObjectFlags.IsNeverIntersectionComputed |
(some(getPropertiesOfUnionOrIntersectionType(<IntersectionType>type), isDiscriminantWithNeverType) ? ObjectFlags.IsNeverIntersection : 0);
}
return (<IntersectionType>type).objectFlags & ObjectFlags.IsNeverIntersection ? neverType : type;
if ((<IntersectionType>type).objectFlags & ObjectFlags.IsNeverIntersection) {
return neverType;
}
return (<IntersectionType>type).resolvedReducedType || ((<IntersectionType>type).resolvedReducedType = getReducedIntersectionType(<IntersectionType>type));
}
else if (type.flags & TypeFlags.Conditional) {
return getReducedConditionalType(type as ConditionalType);
}
return type;
}
Expand All @@ -10377,6 +10384,18 @@ namespace ts {
return reduced;
}

function getReducedIntersectionType(type: IntersectionType) {
const reducedTypes = sameMap(type.types, getReducedType);
if (reducedTypes === type.types) {
return type;
}
const reduced = getIntersectionType(reducedTypes);
if (reduced.flags & TypeFlags.Intersection) {
(<IntersectionType>reduced).resolvedReducedType = reduced;
}
return reduced;
}

function isDiscriminantWithNeverType(prop: Symbol) {
return !(prop.flags & SymbolFlags.Optional) &&
(getCheckFlags(prop) & (CheckFlags.Discriminant | CheckFlags.HasNeverType)) === CheckFlags.Discriminant &&
Expand Down Expand Up @@ -10430,7 +10449,7 @@ namespace ts {
* maps primitive types and type parameters are to their apparent types.
*/
function getSignaturesOfType(type: Type, kind: SignatureKind): readonly Signature[] {
return getSignaturesOfStructuredType(getApparentType(getReducedType(type)), kind);
return getSignaturesOfStructuredType(getApparentType(type), kind);
}

function getIndexInfoOfStructuredType(type: Type, kind: IndexKind): IndexInfo | undefined {
Expand All @@ -10448,13 +10467,13 @@ namespace ts {
// Return the indexing info of the given kind in the given type. Creates synthetic union index types when necessary and
// maps primitive types and type parameters are to their apparent types.
function getIndexInfoOfType(type: Type, kind: IndexKind): IndexInfo | undefined {
return getIndexInfoOfStructuredType(getApparentType(getReducedType(type)), kind);
return getIndexInfoOfStructuredType(getApparentType(type), kind);
}

// Return the index type of the given kind in the given type. Creates synthetic union index types when necessary and
// maps primitive types and type parameters are to their apparent types.
function getIndexTypeOfType(type: Type, kind: IndexKind): Type | undefined {
return getIndexTypeOfStructuredType(getApparentType(getReducedType(type)), kind);
return getIndexTypeOfStructuredType(getApparentType(type), kind);
}

function getImplicitIndexTypeOfType(type: Type, kind: IndexKind): Type | undefined {
Expand Down Expand Up @@ -12035,7 +12054,7 @@ namespace ts {
}
}
const objectFlags = (includes & TypeFlags.NotPrimitiveUnion ? 0 : ObjectFlags.PrimitiveUnion) |
(includes & TypeFlags.Intersection ? ObjectFlags.ContainsIntersections : 0);
(includes & TypeFlags.ReducibleNotUnion ? ObjectFlags.ContainsReducibles : 0);
return getUnionTypeFromSortedList(typeSet, objectFlags, aliasSymbol, aliasTypeArguments);
}

Expand Down Expand Up @@ -12750,6 +12769,25 @@ namespace ts {
return type[cache] = type;
}

function getReducedConditionalType(type: ConditionalType): Type {
const checkType = type.checkType;
const extendsType = getInferredExtendsTypeOfConditional(type);

if (!isGenericObjectType(checkType) && !isGenericIndexType(checkType) && !isGenericObjectType(extendsType) && !isGenericIndexType(extendsType)) {
const result = getConditionalSimplificationState(checkType, extendsType);
switch (result) {
case ConditionalSimplificationState.True:
return getReducedType(getInferredTrueTypeFromConditionalType(type));
case ConditionalSimplificationState.False:
return getReducedType(getFalseTypeFromConditionalType(type));
case ConditionalSimplificationState.Both:
return getUnionType([getReducedType(getInferredTrueTypeFromConditionalType(type)), getReducedType(getFalseTypeFromConditionalType(type))]);
// None: Fall out and return `type`
}
}
return type;
}

function getSimplifiedConditionalType(type: ConditionalType, writing: boolean) {
const checkType = type.checkType;
const extendsType = type.extendsType;
Expand Down Expand Up @@ -12821,7 +12859,7 @@ namespace ts {
// In the following we resolve T[K] to the type of the property in T selected by K.
// We treat boolean as different from other unions to improve errors;
// skipping straight to getPropertyTypeForIndexType gives errors with 'boolean' instead of 'true'.
const apparentObjectType = getApparentType(getReducedType(objectType));
const apparentObjectType = getApparentType(objectType);
if (indexType.flags & TypeFlags.Union && !(indexType.flags & TypeFlags.Boolean)) {
const propTypes: Type[] = [];
let wasMissingProp = false;
Expand Down Expand Up @@ -12888,6 +12926,14 @@ namespace ts {
return type;
}

function isTypeDeferredTypeReference(type: Type) {
return !!(getObjectFlags(type) & ObjectFlags.Reference) && !!(type as TypeReference).node;
}

function getInferredExtendsTypeOfConditional(type: ConditionalType) {
return instantiateType(type.root.extendsType, type.combinedMapper || type.mapper);
}

function getConditionalType(root: ConditionalRoot, mapper: TypeMapper | undefined): Type {
const checkType = instantiateType(root.checkType, mapper);
const extendsType = instantiateType(root.extendsType, mapper);
Expand All @@ -12913,28 +12959,17 @@ namespace ts {
// Instantiate the extends type including inferences for 'infer T' type parameters
const inferredExtendsType = combinedMapper ? instantiateType(root.extendsType, combinedMapper) : extendsType;
// We attempt to resolve the conditional type only when the check and extends types are non-generic
if (!checkTypeInstantiable && !isGenericObjectType(inferredExtendsType) && !isGenericIndexType(inferredExtendsType)) {
if (inferredExtendsType.flags & TypeFlags.AnyOrUnknown) {
return instantiateType(root.trueType, combinedMapper || mapper);
}
// Return union of trueType and falseType for 'any' since it matches anything
if (checkType.flags & TypeFlags.Any) {
return getUnionType([instantiateType(root.trueType, combinedMapper || mapper), instantiateType(root.falseType, mapper)]);
}
// Return falseType for a definitely false extends check. We check an instantiations of the two
// types with type parameters mapped to the wildcard type, the most permissive instantiations
// possible (the wildcard type is assignable to and from all types). If those are not related,
// then no instantiations will be and we can just return the false branch type.
if (!isTypeAssignableTo(getPermissiveInstantiation(checkType), getPermissiveInstantiation(inferredExtendsType))) {
return instantiateType(root.falseType, mapper);
}
// Return trueType for a definitely true extends check. We check instantiations of the two
// types with type parameters mapped to their restrictive form, i.e. a form of the type parameter
// that has no constraint. This ensures that, for example, the type
// type Foo<T extends { x: any }> = T extends { x: string } ? string : number
// doesn't immediately resolve to 'string' instead of being deferred.
if (isTypeAssignableTo(getRestrictiveInstantiation(checkType), getRestrictiveInstantiation(inferredExtendsType))) {
return instantiateType(root.trueType, combinedMapper || mapper);
if (!checkTypeInstantiable && !isGenericObjectType(inferredExtendsType) && !isGenericIndexType(inferredExtendsType)
&& !isTypeDeferredTypeReference(checkType) && !isTypeDeferredTypeReference(inferredExtendsType)) {
const result = getConditionalSimplificationState(checkType, inferredExtendsType);
switch (result) {
case ConditionalSimplificationState.True:
return instantiateType(root.trueType, combinedMapper || mapper);
case ConditionalSimplificationState.Both:
return getUnionType([instantiateType(root.trueType, combinedMapper || mapper), instantiateType(root.falseType, mapper)]);
case ConditionalSimplificationState.False:
return instantiateType(root.falseType, mapper);
// None: Fall out and defer
}
}
// Return a deferred type for a check that is neither definitely true nor definitely false
Expand All @@ -12950,6 +12985,39 @@ namespace ts {
return result;
}

const enum ConditionalSimplificationState {
None = 0,
True = 1,
False = 2,
Both = True | False,
}

function getConditionalSimplificationState(checkType: Type, inferredExtendsType: Type): ConditionalSimplificationState {
if (inferredExtendsType.flags & TypeFlags.AnyOrUnknown) {
return ConditionalSimplificationState.True;
}
// Return union of trueType and falseType for 'any' since it matches anything
if (checkType.flags & TypeFlags.Any) {
return ConditionalSimplificationState.Both;
}
// Return falseType for a definitely false extends check. We check an instantiations of the two
// types with type parameters mapped to the wildcard type, the most permissive instantiations
// possible (the wildcard type is assignable to and from all types). If those are not related,
// then no instantiations will be and we can just return the false branch type.
if (!isTypeAssignableTo(getPermissiveInstantiation(checkType), getPermissiveInstantiation(inferredExtendsType))) {
return ConditionalSimplificationState.False;
}
// Return trueType for a definitely true extends check. We check instantiations of the two
// types with type parameters mapped to their restrictive form, i.e. a form of the type parameter
// that has no constraint. This ensures that, for example, the type
// type Foo<T extends { x: any }> = T extends { x: string } ? string : number
// doesn't immediately resolve to 'string' instead of being deferred.
if (isTypeAssignableTo(getRestrictiveInstantiation(checkType), getRestrictiveInstantiation(inferredExtendsType))) {
return ConditionalSimplificationState.True;
}
return ConditionalSimplificationState.None;
}

function getTrueTypeFromConditionalType(type: ConditionalType) {
return type.resolvedTrueType || (type.resolvedTrueType = instantiateType(type.root.trueType, type.mapper));
}
Expand Down Expand Up @@ -14986,7 +15054,7 @@ namespace ts {
while (true) {
const t = isFreshLiteralType(type) ? (<FreshableType>type).regularType :
getObjectFlags(type) & ObjectFlags.Reference && (<TypeReference>type).node ? createTypeReference((<TypeReference>type).target, getTypeArguments(<TypeReference>type)) :
type.flags & TypeFlags.UnionOrIntersection ? getReducedType(type) :
type.flags & TypeFlags.Reducible ? getSimplifiedType(getReducedType(type), writing) :
type.flags & TypeFlags.Substitution ? writing ? (<SubstitutionType>type).baseType : (<SubstitutionType>type).substitute :
type.flags & TypeFlags.Simplifiable ? getSimplifiedType(type, writing) :
type;
Expand Down
14 changes: 9 additions & 5 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4376,6 +4376,10 @@ namespace ts {
/* @internal */
Simplifiable = IndexedAccess | Conditional,
/* @internal */
ReducibleNotUnion = Intersection | Conditional,
/* @internal */
Reducible = ReducibleNotUnion | Union,
/* @internal */
Substructure = Object | Union | Intersection | Index | IndexedAccess | Conditional | Substitution,
// 'Narrowable' types are types where narrowing actually narrows.
// This *should* be every type other than null, undefined, void, and never
Expand All @@ -4385,7 +4389,7 @@ namespace ts {
NotPrimitiveUnion = Any | Unknown | Enum | Void | Never | StructuredOrInstantiable,
// The following flags are aggregated during union and intersection type construction
/* @internal */
IncludesMask = Any | Unknown | Primitive | Never | Object | Union | Intersection | NonPrimitive,
IncludesMask = Any | Unknown | Primitive | Never | Object | Union | Intersection | NonPrimitive | Conditional,
// The following flags are used for different purposes during union and intersection type construction
/* @internal */
IncludesStructuredOrInstantiable = TypeParameter,
Expand All @@ -4394,7 +4398,7 @@ namespace ts {
/* @internal */
IncludesWildcard = IndexedAccess,
/* @internal */
IncludesEmptyObject = Conditional,
IncludesEmptyObject = 1 << 27,
}

export type DestructuringPattern = BindingPattern | ObjectLiteralExpression | ArrayLiteralExpression;
Expand Down Expand Up @@ -4511,7 +4515,7 @@ namespace ts {
/* @internal */
CouldContainTypeVariables = 1 << 27, // Type could contain a type variable
/* @internal */
ContainsIntersections = 1 << 28, // Union contains intersections
ContainsReducibles = 1 << 28, // Union contains intersections
/* @internal */
IsNeverIntersectionComputed = 1 << 28, // IsNeverLike flag has been computed
/* @internal */
Expand Down Expand Up @@ -4634,11 +4638,11 @@ namespace ts {
resolvedStringIndexType: IndexType;
/* @internal */
resolvedBaseConstraint: Type;
/* @internal */
resolvedReducedType: Type;
}

export interface UnionType extends UnionOrIntersectionType {
/* @internal */
resolvedReducedType: Type;
}

export interface IntersectionType extends UnionOrIntersectionType {
Expand Down
66 changes: 66 additions & 0 deletions tests/baselines/reference/recursiveArrayNotCircular.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//// [recursiveArrayNotCircular.ts]
type Action<T, P> = P extends void ? { type : T } : { type: T, payload: P }

enum ActionType {
Foo,
Bar,
Baz,
Batch
}

type ReducerAction =
| Action<ActionType.Bar, number>
| Action<ActionType.Baz, boolean>
| Action<ActionType.Foo, string>
| Action<ActionType.Batch, ReducerAction[]>

function assertNever(a: never): never {
throw new Error("Unreachable!");
}

function reducer(action: ReducerAction): void {
switch(action.type) {
case ActionType.Bar:
const x: number = action.payload;
break;
case ActionType.Baz:
const y: boolean = action.payload;
break;
case ActionType.Foo:
const z: string = action.payload;
break;
case ActionType.Batch:
action.payload.map(reducer);
break;
default: return assertNever(action);
}
}

//// [recursiveArrayNotCircular.js]
var ActionType;
(function (ActionType) {
ActionType[ActionType["Foo"] = 0] = "Foo";
ActionType[ActionType["Bar"] = 1] = "Bar";
ActionType[ActionType["Baz"] = 2] = "Baz";
ActionType[ActionType["Batch"] = 3] = "Batch";
})(ActionType || (ActionType = {}));
function assertNever(a) {
throw new Error("Unreachable!");
}
function reducer(action) {
switch (action.type) {
case ActionType.Bar:
var x = action.payload;
break;
case ActionType.Baz:
var y = action.payload;
break;
case ActionType.Foo:
var z = action.payload;
break;
case ActionType.Batch:
action.payload.map(reducer);
break;
default: return assertNever(action);
}
}
Loading