Skip to content

Make special intersections order-independent #52782

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
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
6 changes: 5 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16649,12 +16649,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return reduceLeft(types, (n, t) => n + getConstituentCount(t), 0);
}

function areIntersectedTypesAvoidingPrimitiveReduction(t1: Type, t2: Type) {
return !!(t1.flags & (TypeFlags.String | TypeFlags.Number | TypeFlags.BigInt)) && t2 === emptyTypeLiteralType;
}

function getTypeFromIntersectionTypeNode(node: IntersectionTypeNode): Type {
const links = getNodeLinks(node);
if (!links.resolvedType) {
const aliasSymbol = getAliasSymbolForTypeNode(node);
const types = map(node.types, getTypeFromTypeNode);
const noSupertypeReduction = types.length === 2 && !!(types[0].flags & (TypeFlags.String | TypeFlags.Number | TypeFlags.BigInt)) && types[1] === emptyTypeLiteralType;
const noSupertypeReduction = types.length === 2 && (areIntersectedTypesAvoidingPrimitiveReduction(types[0], types[1]) || areIntersectedTypesAvoidingPrimitiveReduction(types[1], types[0]));
links.resolvedType = getIntersectionType(types, aliasSymbol, getTypeArgumentsForAliasSymbol(aliasSymbol), noSupertypeReduction);
}
return links.resolvedType;
Expand Down
8 changes: 6 additions & 2 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2163,15 +2163,19 @@ export function isStringOrRegularExpressionOrTemplateLiteral(kind: SyntaxKind):
return false;
}

function areIntersectedTypesAvoidingStringReduction(checker: TypeChecker, t1: Type, t2: Type) {
return !!(t1.flags & TypeFlags.String) && checker.isEmptyAnonymousObjectType(t2);
}

/** @internal */
export function isStringAndEmptyAnonymousObjectIntersection(type: Type) {
if (!type.isIntersection()) {
return false;
}

const { types, checker } = type;
return types.length === 2
&& (types[0].flags & TypeFlags.String) && checker.isEmptyAnonymousObjectType(types[1]);
return types.length === 2 &&
(areIntersectedTypesAvoidingStringReduction(checker, types[0], types[1]) || areIntersectedTypesAvoidingStringReduction(checker, types[1], types[0]));
}

/** @internal */
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/unknownControlFlow.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ tests/cases/conformance/types/unknown/unknownControlFlow.ts(293,5): error TS2345


==== tests/cases/conformance/types/unknown/unknownControlFlow.ts (5 errors) ====
type T01 = {} & string; // string
type T01 = {} & string; // {} & string
type T02 = {} & 'a'; // 'a'
type T03 = {} & object; // object
type T04 = {} & { x: number }; // { x: number }
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/unknownControlFlow.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//// [unknownControlFlow.ts]
type T01 = {} & string; // string
type T01 = {} & string; // {} & string
type T02 = {} & 'a'; // 'a'
type T03 = {} & object; // object
type T04 = {} & { x: number }; // { x: number }
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/unknownControlFlow.symbols
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
=== tests/cases/conformance/types/unknown/unknownControlFlow.ts ===
type T01 = {} & string; // string
type T01 = {} & string; // {} & string
>T01 : Symbol(T01, Decl(unknownControlFlow.ts, 0, 0))

type T02 = {} & 'a'; // 'a'
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/unknownControlFlow.types
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=== tests/cases/conformance/types/unknown/unknownControlFlow.ts ===
type T01 = {} & string; // string
>T01 : string
type T01 = {} & string; // {} & string
>T01 : {} & string

type T02 = {} & 'a'; // 'a'
>T02 : "a"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @strict: true
// @declaration: true

type T01 = {} & string; // string
type T01 = {} & string; // {} & string
type T02 = {} & 'a'; // 'a'
type T03 = {} & object; // object
type T04 = {} & { x: number }; // { x: number }
Expand Down
8 changes: 8 additions & 0 deletions tests/cases/fourslash/specialIntersectionsOrderIndependent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference path="fourslash.ts" />
//
//// declare function a(arg: 'test' | (string & {})): void
//// a('/*1*/')
//// declare function b(arg: 'test' | ({} & string)): void
//// b('/*2*/')

verify.completions({ marker: ["1", "2"], exact: ["test"] });