Skip to content

Remove redundant primitive types from intersections #23751

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

Merged
merged 5 commits into from
Apr 28, 2018
Merged
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
151 changes: 72 additions & 79 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,22 +617,6 @@ namespace ts {
Both = Source | Target,
}

const enum TypeIncludes {
Any = 1 << 0,
Undefined = 1 << 1,
Null = 1 << 2,
Never = 1 << 3,
NonWideningType = 1 << 4,
String = 1 << 5,
Number = 1 << 6,
ESSymbol = 1 << 7,
LiteralOrUniqueESSymbol = 1 << 8,
ObjectType = 1 << 9,
EmptyObject = 1 << 10,
Union = 1 << 11,
Wildcard = 1 << 12,
}

const enum MembersOrExportsResolutionKind {
resolvedExports = "resolvedExports",
resolvedMembers = "resolvedMembers"
Expand Down Expand Up @@ -8019,35 +8003,31 @@ namespace ts {
return false;
}

function addTypeToUnion(typeSet: Type[], includes: TypeIncludes, type: Type) {
function addTypeToUnion(typeSet: Type[], includes: TypeFlags, type: Type) {
const flags = type.flags;
if (flags & TypeFlags.Union) {
includes = addTypesToUnion(typeSet, includes, (<UnionType>type).types);
}
else if (flags & TypeFlags.Any) {
includes |= TypeIncludes.Any;
if (type === wildcardType) includes |= TypeIncludes.Wildcard;
}
else if (!strictNullChecks && flags & TypeFlags.Nullable) {
if (flags & TypeFlags.Undefined) includes |= TypeIncludes.Undefined;
if (flags & TypeFlags.Null) includes |= TypeIncludes.Null;
if (!(flags & TypeFlags.ContainsWideningType)) includes |= TypeIncludes.NonWideningType;
}
else if (!(flags & TypeFlags.Never || flags & TypeFlags.Intersection && isEmptyIntersectionType(<IntersectionType>type))) {
// We ignore 'never' types in unions. Likewise, we ignore intersections of unit types as they are
// another form of 'never' (in that they have an empty value domain). We could in theory turn
// intersections of unit types into 'never' upon construction, but deferring the reduction makes it
// easier to reason about their origin.
if (flags & TypeFlags.String) includes |= TypeIncludes.String;
if (flags & TypeFlags.Number) includes |= TypeIncludes.Number;
if (flags & TypeFlags.ESSymbol) includes |= TypeIncludes.ESSymbol;
if (flags & TypeFlags.StringOrNumberLiteralOrUnique) includes |= TypeIncludes.LiteralOrUniqueESSymbol;
const len = typeSet.length;
const index = len && type.id > typeSet[len - 1].id ? ~len : binarySearch(typeSet, type, getTypeId, compareValues);
if (index < 0) {
if (!(flags & TypeFlags.Object && (<ObjectType>type).objectFlags & ObjectFlags.Anonymous &&
type.symbol && type.symbol.flags & (SymbolFlags.Function | SymbolFlags.Method) && containsIdenticalType(typeSet, type))) {
typeSet.splice(~index, 0, type);
return addTypesToUnion(typeSet, includes, (<UnionType>type).types);
}
// We ignore 'never' types in unions. Likewise, we ignore intersections of unit types as they are
// another form of 'never' (in that they have an empty value domain). We could in theory turn
// intersections of unit types into 'never' upon construction, but deferring the reduction makes it
// easier to reason about their origin.
if (!(flags & TypeFlags.Never || flags & TypeFlags.Intersection && isEmptyIntersectionType(<IntersectionType>type))) {
includes |= flags & ~TypeFlags.ConstructionFlags;
if (flags & TypeFlags.Any) {
if (type === wildcardType) includes |= TypeFlags.Wildcard;
}
else if (!strictNullChecks && flags & TypeFlags.Nullable) {
if (!(flags & TypeFlags.ContainsWideningType)) includes |= TypeFlags.NonWideningType;
}
else {
const len = typeSet.length;
const index = len && type.id > typeSet[len - 1].id ? ~len : binarySearch(typeSet, type, getTypeId, compareValues);
if (index < 0) {
if (!(flags & TypeFlags.Object && (<ObjectType>type).objectFlags & ObjectFlags.Anonymous &&
type.symbol && type.symbol.flags & (SymbolFlags.Function | SymbolFlags.Method) && containsIdenticalType(typeSet, type))) {
typeSet.splice(~index, 0, type);
}
}
}
}
Expand All @@ -8056,7 +8036,7 @@ namespace ts {

// Add the given types to the given type set. Order is preserved, duplicates are removed,
// and nested types of the given kind are flattened into the set.
function addTypesToUnion(typeSet: Type[], includes: TypeIncludes, types: Type[]): TypeIncludes {
function addTypesToUnion(typeSet: Type[], includes: TypeFlags, types: Type[]): TypeFlags {
for (const type of types) {
includes = addTypeToUnion(typeSet, includes, type);
}
Expand Down Expand Up @@ -8113,15 +8093,15 @@ namespace ts {
}
}

function removeRedundantLiteralTypes(types: Type[], includes: TypeIncludes) {
function removeRedundantLiteralTypes(types: Type[], includes: TypeFlags) {
let i = types.length;
while (i > 0) {
i--;
const t = types[i];
const remove =
t.flags & TypeFlags.StringLiteral && includes & TypeIncludes.String ||
t.flags & TypeFlags.NumberLiteral && includes & TypeIncludes.Number ||
t.flags & TypeFlags.UniqueESSymbol && includes & TypeIncludes.ESSymbol ||
t.flags & TypeFlags.StringLiteral && includes & TypeFlags.String ||
t.flags & TypeFlags.NumberLiteral && includes & TypeFlags.Number ||
t.flags & TypeFlags.UniqueESSymbol && includes & TypeFlags.ESSymbol ||
t.flags & TypeFlags.StringOrNumberLiteral && t.flags & TypeFlags.FreshLiteral && containsType(types, (<LiteralType>t).regularType);
if (remove) {
orderedRemoveItemAt(types, i);
Expand All @@ -8145,12 +8125,12 @@ namespace ts {
}
const typeSet: Type[] = [];
const includes = addTypesToUnion(typeSet, 0, types);
if (includes & TypeIncludes.Any) {
return includes & TypeIncludes.Wildcard ? wildcardType : anyType;
if (includes & TypeFlags.Any) {
return includes & TypeFlags.Wildcard ? wildcardType : anyType;
}
switch (unionReduction) {
case UnionReduction.Literal:
if (includes & TypeIncludes.LiteralOrUniqueESSymbol) {
if (includes & TypeFlags.StringOrNumberLiteralOrUnique) {
removeRedundantLiteralTypes(typeSet, includes);
}
break;
Expand All @@ -8159,8 +8139,8 @@ namespace ts {
break;
}
if (typeSet.length === 0) {
return includes & TypeIncludes.Null ? includes & TypeIncludes.NonWideningType ? nullType : nullWideningType :
includes & TypeIncludes.Undefined ? includes & TypeIncludes.NonWideningType ? undefinedType : undefinedWideningType :
return includes & TypeFlags.Null ? includes & TypeFlags.NonWideningType ? nullType : nullWideningType :
includes & TypeFlags.Undefined ? includes & TypeFlags.NonWideningType ? undefinedType : undefinedWideningType :
neverType;
}
return getUnionTypeFromSortedList(typeSet, aliasSymbol, aliasTypeArguments);
Expand Down Expand Up @@ -8238,30 +8218,23 @@ namespace ts {
return links.resolvedType;
}

function addTypeToIntersection(typeSet: Type[], includes: TypeIncludes, type: Type) {
function addTypeToIntersection(typeSet: Type[], includes: TypeFlags, type: Type) {
const flags = type.flags;
if (flags & TypeFlags.Intersection) {
includes = addTypesToIntersection(typeSet, includes, (<IntersectionType>type).types);
}
else if (flags & TypeFlags.Any) {
includes |= TypeIncludes.Any;
if (type === wildcardType) includes |= TypeIncludes.Wildcard;
}
else if (flags & TypeFlags.Never) {
includes |= TypeIncludes.Never;
return addTypesToIntersection(typeSet, includes, (<IntersectionType>type).types);
}
else if (getObjectFlags(type) & ObjectFlags.Anonymous && isEmptyObjectType(type)) {
includes |= TypeIncludes.EmptyObject;
if (getObjectFlags(type) & ObjectFlags.Anonymous && isEmptyObjectType(type)) {
includes |= TypeFlags.EmptyObject;
}
else if ((strictNullChecks || !(flags & TypeFlags.Nullable)) && !contains(typeSet, type)) {
if (flags & TypeFlags.Object) {
includes |= TypeIncludes.ObjectType;
}
if (flags & TypeFlags.Union) {
includes |= TypeIncludes.Union;
}
if (!(flags & TypeFlags.Object && (<ObjectType>type).objectFlags & ObjectFlags.Anonymous &&
type.symbol && type.symbol.flags & (SymbolFlags.Function | SymbolFlags.Method) && containsIdenticalType(typeSet, type))) {
else {
includes |= flags & ~TypeFlags.ConstructionFlags;
if (flags & TypeFlags.Any) {
if (type === wildcardType) includes |= TypeFlags.Wildcard;
}
else if ((strictNullChecks || !(flags & TypeFlags.Nullable)) && !contains(typeSet, type) &&
!(flags & TypeFlags.Object && (<ObjectType>type).objectFlags & ObjectFlags.Anonymous &&
type.symbol && type.symbol.flags & (SymbolFlags.Function | SymbolFlags.Method) &&
containsIdenticalType(typeSet, type))) {
typeSet.push(type);
}
}
Expand All @@ -8270,13 +8243,28 @@ namespace ts {

// Add the given types to the given type set. Order is preserved, freshness is removed from literal
// types, duplicates are removed, and nested types of the given kind are flattened into the set.
function addTypesToIntersection(typeSet: Type[], includes: TypeIncludes, types: Type[]) {
function addTypesToIntersection(typeSet: Type[], includes: TypeFlags, types: Type[]) {
for (const type of types) {
includes = addTypeToIntersection(typeSet, includes, getRegularTypeOfLiteralType(type));
}
return includes;
}

function removeRedundantPrimitiveTypes(types: Type[], includes: TypeFlags) {
let i = types.length;
while (i > 0) {
i--;
const t = types[i];
const remove =
t.flags & TypeFlags.String && includes & TypeFlags.StringLiteral ||
t.flags & TypeFlags.Number && includes & TypeFlags.NumberLiteral ||
t.flags & TypeFlags.ESSymbol && includes & TypeFlags.UniqueESSymbol;
if (remove) {
orderedRemoveItemAt(types, i);
}
}
}

// We normalize combinations of intersection and union types based on the distributive property of the '&'
// operator. Specifically, because X & (A | B) is equivalent to X & A | X & B, we can transform intersection
// types with union type constituents into equivalent union types with intersection type constituents and
Expand All @@ -8293,19 +8281,24 @@ namespace ts {
}
const typeSet: Type[] = [];
const includes = addTypesToIntersection(typeSet, 0, types);
if (includes & TypeIncludes.Never) {
if (includes & TypeFlags.Never) {
return neverType;
}
if (includes & TypeIncludes.Any) {
return includes & TypeIncludes.Wildcard ? wildcardType : anyType;
if (includes & TypeFlags.Any) {
return includes & TypeFlags.Wildcard ? wildcardType : anyType;
}
if (includes & TypeFlags.String && includes & TypeFlags.StringLiteral ||
includes & TypeFlags.Number && includes & TypeFlags.NumberLiteral ||
includes & TypeFlags.ESSymbol && includes & TypeFlags.UniqueESSymbol) {
removeRedundantPrimitiveTypes(typeSet, includes);
}
if (includes & TypeIncludes.EmptyObject && !(includes & TypeIncludes.ObjectType)) {
if (includes & TypeFlags.EmptyObject && !(includes & TypeFlags.Object)) {
typeSet.push(emptyObjectType);
}
if (typeSet.length === 1) {
return typeSet[0];
}
if (includes & TypeIncludes.Union) {
if (includes & TypeFlags.Union) {
// We are attempting to construct a type of the form X & (A | B) & Y. Transform this into a type of
// the form X & A & Y | X & B & Y and recursively reduce until no union type constituents remain.
const unionIndex = findIndex(typeSet, t => (t.flags & TypeFlags.Union) !== 0);
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3639,7 +3639,15 @@ namespace ts {
RequiresWidening = ContainsWideningType | ContainsObjectLiteral,
/* @internal */
PropagatingFlags = ContainsWideningType | ContainsObjectLiteral | ContainsAnyFunctionType,
// The following flags are used for different purposes during union and intersection type construction
/* @internal */
NonWideningType = ContainsWideningType,
/* @internal */
Wildcard = ContainsObjectLiteral,
/* @internal */
EmptyObject = ContainsAnyFunctionType,
/* @internal */
ConstructionFlags = NonWideningType | Wildcard | EmptyObject
}

export type DestructuringPattern = BindingPattern | ObjectLiteralExpression | ArrayLiteralExpression;
Expand Down
20 changes: 20 additions & 0 deletions tests/baselines/reference/intersectionReduction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [intersectionReduction.ts]
// @strict

declare const sym1: unique symbol;
declare const sym2: unique symbol;

type T1 = string & 'a'; // 'a'
type T2 = 'a' & string & 'b'; // 'a' & 'b'
type T3 = number & 10; // 10
type T4 = 10 & number & 20; // 10 & 20
type T5 = symbol & typeof sym1; // typeof sym1
type T6 = typeof sym1 & symbol & typeof sym2; // typeof sym1 & typeof sym2
type T7 = string & 'a' & number & 10 & symbol & typeof sym1; // 'a' & 10 & typeof sym1

type T10 = string & ('a' | 'b'); // 'a' | 'b'
type T11 = (string | number) & ('a' | 10); // 'a' | 10


//// [intersectionReduction.js]
// @strict
40 changes: 40 additions & 0 deletions tests/baselines/reference/intersectionReduction.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
=== tests/cases/conformance/types/intersection/intersectionReduction.ts ===
// @strict

declare const sym1: unique symbol;
>sym1 : Symbol(sym1, Decl(intersectionReduction.ts, 2, 13))

declare const sym2: unique symbol;
>sym2 : Symbol(sym2, Decl(intersectionReduction.ts, 3, 13))

type T1 = string & 'a'; // 'a'
>T1 : Symbol(T1, Decl(intersectionReduction.ts, 3, 34))

type T2 = 'a' & string & 'b'; // 'a' & 'b'
>T2 : Symbol(T2, Decl(intersectionReduction.ts, 5, 23))

type T3 = number & 10; // 10
>T3 : Symbol(T3, Decl(intersectionReduction.ts, 6, 29))

type T4 = 10 & number & 20; // 10 & 20
>T4 : Symbol(T4, Decl(intersectionReduction.ts, 7, 22))

type T5 = symbol & typeof sym1; // typeof sym1
>T5 : Symbol(T5, Decl(intersectionReduction.ts, 8, 27))
>sym1 : Symbol(sym1, Decl(intersectionReduction.ts, 2, 13))

type T6 = typeof sym1 & symbol & typeof sym2; // typeof sym1 & typeof sym2
>T6 : Symbol(T6, Decl(intersectionReduction.ts, 9, 31))
>sym1 : Symbol(sym1, Decl(intersectionReduction.ts, 2, 13))
>sym2 : Symbol(sym2, Decl(intersectionReduction.ts, 3, 13))

type T7 = string & 'a' & number & 10 & symbol & typeof sym1; // 'a' & 10 & typeof sym1
>T7 : Symbol(T7, Decl(intersectionReduction.ts, 10, 45))
>sym1 : Symbol(sym1, Decl(intersectionReduction.ts, 2, 13))

type T10 = string & ('a' | 'b'); // 'a' | 'b'
>T10 : Symbol(T10, Decl(intersectionReduction.ts, 11, 60))

type T11 = (string | number) & ('a' | 10); // 'a' | 10
>T11 : Symbol(T11, Decl(intersectionReduction.ts, 13, 32))

40 changes: 40 additions & 0 deletions tests/baselines/reference/intersectionReduction.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
=== tests/cases/conformance/types/intersection/intersectionReduction.ts ===
// @strict

declare const sym1: unique symbol;
>sym1 : unique symbol

declare const sym2: unique symbol;
>sym2 : unique symbol

type T1 = string & 'a'; // 'a'
>T1 : "a"

type T2 = 'a' & string & 'b'; // 'a' & 'b'
>T2 : T2

type T3 = number & 10; // 10
>T3 : 10

type T4 = 10 & number & 20; // 10 & 20
>T4 : T4

type T5 = symbol & typeof sym1; // typeof sym1
>T5 : unique symbol
>sym1 : unique symbol

type T6 = typeof sym1 & symbol & typeof sym2; // typeof sym1 & typeof sym2
>T6 : T6
>sym1 : unique symbol
>sym2 : unique symbol

type T7 = string & 'a' & number & 10 & symbol & typeof sym1; // 'a' & 10 & typeof sym1
>T7 : T7
>sym1 : unique symbol

type T10 = string & ('a' | 'b'); // 'a' | 'b'
>T10 : "a" | "b"

type T11 = (string | number) & ('a' | 10); // 'a' | 10
>T11 : "a" | 10

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @strict
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// @strict: true
This isn't setting any compiler options as is and becomes just a comment in the output. Though I guess it's heartening to know that nothing in this change actually relies on strict. 😛

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. We have hundreds of tests that include // @strict: true and they definitely behave differently if you remove the comment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

....this comment isn't // @strict: true, it's just // @strict. This comment does nothing.


declare const sym1: unique symbol;
declare const sym2: unique symbol;

type T1 = string & 'a'; // 'a'
type T2 = 'a' & string & 'b'; // 'a' & 'b'
type T3 = number & 10; // 10
type T4 = 10 & number & 20; // 10 & 20
type T5 = symbol & typeof sym1; // typeof sym1
type T6 = typeof sym1 & symbol & typeof sym2; // typeof sym1 & typeof sym2
type T7 = string & 'a' & number & 10 & symbol & typeof sym1; // 'a' & 10 & typeof sym1

type T10 = string & ('a' | 'b'); // 'a' | 'b'
type T11 = (string | number) & ('a' | 10); // 'a' | 10