Skip to content

Add union and intersection types to unions by type list id #54554

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
12 changes: 9 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16593,7 +16593,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
function addTypeToIntersection(typeSet: Map<string, Type>, includes: TypeFlags, type: Type) {
const flags = type.flags;
if (flags & TypeFlags.Intersection) {
return addTypesToIntersection(typeSet, includes, (type as IntersectionType).types);
const list = `&${getTypeListId((type as IntersectionType).types)}`;
if (!typeSet.has(list)) {
typeSet.set(list, unknownType); // set the set as containing this intersection, so later copies aren't iterated over - `unknown` should be a noop set member
return addTypesToIntersection(typeSet, includes, (type as IntersectionType).types);
}
return includes;
}
if (isEmptyAnonymousObjectType(type)) {
if (!(includes & TypeFlags.IncludesEmptyObject)) {
Expand All @@ -16610,13 +16615,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
includes |= TypeFlags.IncludesMissingType;
type = undefinedType;
}
if (!typeSet.has(type.id.toString())) {
const id = type.flags & TypeFlags.Union ? `|${getTypeListId((type as UnionType).types)}` : type.id.toString();
if (!typeSet.has(id)) {
if (type.flags & TypeFlags.Unit && includes & TypeFlags.Unit) {
// We have seen two distinct unit types which means we should reduce to an
// empty intersection. Adding TypeFlags.NonPrimitive causes that to happen.
includes |= TypeFlags.NonPrimitive;
}
typeSet.set(type.id.toString(), type);
typeSet.set(id, type);
}
}
includes |= flags & TypeFlags.IncludesMask;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//// [intersectionOfIdenticalTypesNotComplex.ts]
// Just a short block to make a union with > 300 unique members
export type Bit = 0 | 1;
export type SyntaxKind = `${Bit}${Bit}${Bit}${Bit}${Bit}${Bit}${Bit}${Bit}${Bit}`
type NodeMaker<T extends SyntaxKind = SyntaxKind> = T extends T ? {kind: T}: never;

type Node = NodeMaker;
type Ok = Node & Node;

type Node2 = NodeMaker;
type Wat = Node & Node2;

//// [intersectionOfIdenticalTypesNotComplex.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
=== tests/cases/compiler/intersectionOfIdenticalTypesNotComplex.ts ===
// Just a short block to make a union with > 300 unique members
export type Bit = 0 | 1;
>Bit : Symbol(Bit, Decl(intersectionOfIdenticalTypesNotComplex.ts, 0, 0))

export type SyntaxKind = `${Bit}${Bit}${Bit}${Bit}${Bit}${Bit}${Bit}${Bit}${Bit}`
>SyntaxKind : Symbol(SyntaxKind, Decl(intersectionOfIdenticalTypesNotComplex.ts, 1, 24))
>Bit : Symbol(Bit, Decl(intersectionOfIdenticalTypesNotComplex.ts, 0, 0))
>Bit : Symbol(Bit, Decl(intersectionOfIdenticalTypesNotComplex.ts, 0, 0))
>Bit : Symbol(Bit, Decl(intersectionOfIdenticalTypesNotComplex.ts, 0, 0))
>Bit : Symbol(Bit, Decl(intersectionOfIdenticalTypesNotComplex.ts, 0, 0))
>Bit : Symbol(Bit, Decl(intersectionOfIdenticalTypesNotComplex.ts, 0, 0))
>Bit : Symbol(Bit, Decl(intersectionOfIdenticalTypesNotComplex.ts, 0, 0))
>Bit : Symbol(Bit, Decl(intersectionOfIdenticalTypesNotComplex.ts, 0, 0))
>Bit : Symbol(Bit, Decl(intersectionOfIdenticalTypesNotComplex.ts, 0, 0))
>Bit : Symbol(Bit, Decl(intersectionOfIdenticalTypesNotComplex.ts, 0, 0))

type NodeMaker<T extends SyntaxKind = SyntaxKind> = T extends T ? {kind: T}: never;
>NodeMaker : Symbol(NodeMaker, Decl(intersectionOfIdenticalTypesNotComplex.ts, 2, 81))
>T : Symbol(T, Decl(intersectionOfIdenticalTypesNotComplex.ts, 3, 15))
>SyntaxKind : Symbol(SyntaxKind, Decl(intersectionOfIdenticalTypesNotComplex.ts, 1, 24))
>SyntaxKind : Symbol(SyntaxKind, Decl(intersectionOfIdenticalTypesNotComplex.ts, 1, 24))
>T : Symbol(T, Decl(intersectionOfIdenticalTypesNotComplex.ts, 3, 15))
>T : Symbol(T, Decl(intersectionOfIdenticalTypesNotComplex.ts, 3, 15))
>kind : Symbol(kind, Decl(intersectionOfIdenticalTypesNotComplex.ts, 3, 67))
>T : Symbol(T, Decl(intersectionOfIdenticalTypesNotComplex.ts, 3, 15))

type Node = NodeMaker;
>Node : Symbol(Node, Decl(intersectionOfIdenticalTypesNotComplex.ts, 3, 83))
>NodeMaker : Symbol(NodeMaker, Decl(intersectionOfIdenticalTypesNotComplex.ts, 2, 81))

type Ok = Node & Node;
>Ok : Symbol(Ok, Decl(intersectionOfIdenticalTypesNotComplex.ts, 5, 22))
>Node : Symbol(Node, Decl(intersectionOfIdenticalTypesNotComplex.ts, 3, 83))
>Node : Symbol(Node, Decl(intersectionOfIdenticalTypesNotComplex.ts, 3, 83))

type Node2 = NodeMaker;
>Node2 : Symbol(Node2, Decl(intersectionOfIdenticalTypesNotComplex.ts, 6, 22))
>NodeMaker : Symbol(NodeMaker, Decl(intersectionOfIdenticalTypesNotComplex.ts, 2, 81))

type Wat = Node & Node2;
>Wat : Symbol(Wat, Decl(intersectionOfIdenticalTypesNotComplex.ts, 8, 23))
>Node : Symbol(Node, Decl(intersectionOfIdenticalTypesNotComplex.ts, 3, 83))
>Node2 : Symbol(Node2, Decl(intersectionOfIdenticalTypesNotComplex.ts, 6, 22))

Loading