Skip to content

Commit 1ac8380

Browse files
committed
Defer union or intersection property type normalization
1 parent 9052804 commit 1ac8380

14 files changed

+471
-8
lines changed

src/compiler/checker.ts

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5911,7 +5911,20 @@ namespace ts {
59115911
return anyType;
59125912
}
59135913

5914+
function getTypeOfSymbolWithDeferredType(symbol: Symbol) {
5915+
const links = getSymbolLinks(symbol);
5916+
if (!links.type) {
5917+
Debug.assertDefined(links.deferralParent);
5918+
Debug.assertDefined(links.deferralConstituents);
5919+
links.type = links.deferralParent!.flags & TypeFlags.Union ? getUnionType(links.deferralConstituents!) : getIntersectionType(links.deferralConstituents!);
5920+
}
5921+
return links.type;
5922+
}
5923+
59145924
function getTypeOfSymbol(symbol: Symbol): Type {
5925+
if (getCheckFlags(symbol) & CheckFlags.DeferredType) {
5926+
return getTypeOfSymbolWithDeferredType(symbol);
5927+
}
59155928
if (getCheckFlags(symbol) & CheckFlags.Instantiated) {
59165929
return getTypeOfInstantiatedSymbol(symbol);
59175930
}
@@ -8046,7 +8059,15 @@ namespace ts {
80468059

80478060
result.declarations = declarations!;
80488061
result.nameType = nameType;
8049-
result.type = isUnion ? getUnionType(propTypes) : getIntersectionType(propTypes);
8062+
if (propTypes.length > 2) {
8063+
// When `propTypes` has the potential to explode in size when normalized, defer normalization until absolutely needed
8064+
result.checkFlags |= CheckFlags.DeferredType;
8065+
result.deferralParent = containingType;
8066+
result.deferralConstituents = propTypes;
8067+
}
8068+
else {
8069+
result.type = isUnion ? getUnionType(propTypes) : getIntersectionType(propTypes);
8070+
}
80508071
return result;
80518072
}
80528073

@@ -12260,8 +12281,8 @@ namespace ts {
1226012281
return false;
1226112282
}
1226212283

12263-
function isIgnoredJsxProperty(source: Type, sourceProp: Symbol, targetMemberType: Type | undefined) {
12264-
return getObjectFlags(source) & ObjectFlags.JsxAttributes && !(isUnhyphenatedJsxName(sourceProp.escapedName) || targetMemberType);
12284+
function isIgnoredJsxProperty(source: Type, sourceProp: Symbol) {
12285+
return getObjectFlags(source) & ObjectFlags.JsxAttributes && !isUnhyphenatedJsxName(sourceProp.escapedName);
1226512286
}
1226612287

1226712288
/**
@@ -13443,6 +13464,49 @@ namespace ts {
1344313464
return result || properties;
1344413465
}
1344513466

13467+
function isPropertySymbolTypeRelated(sourceProp: Symbol, targetProp: Symbol, getTypeOfSourceProperty: (sym: Symbol) => Type, reportErrors: boolean): Ternary {
13468+
const targetIsOptional = strictNullChecks && !!(getCheckFlags(targetProp) & CheckFlags.Partial);
13469+
const source = getTypeOfSourceProperty(sourceProp);
13470+
if (getCheckFlags(targetProp) & CheckFlags.DeferredType && !getSymbolLinks(targetProp).type) {
13471+
// Rather than resolving (and normalizing) the type, relate constituent-by-constituent without performing normalization or seconadary passes
13472+
const links = getSymbolLinks(targetProp);
13473+
Debug.assertDefined(links.deferralParent);
13474+
Debug.assertDefined(links.deferralConstituents);
13475+
const unionParent = !!(links.deferralParent!.flags & TypeFlags.Union);
13476+
let result = unionParent ? Ternary.False : Ternary.True;
13477+
const targetTypes = links.deferralConstituents!;
13478+
for (const targetType of targetTypes) {
13479+
const related = isRelatedTo(source, targetType, /*reportErrors*/ false, /*headMessage*/ undefined, /*isIntersectionConstituent*/ !unionParent);
13480+
if (!unionParent) {
13481+
if (!related) {
13482+
// Can't assign to a target individually - have to fallback to assigning to the _whole_ intersection (which forces normalization)
13483+
return isRelatedTo(source, addOptionality(getTypeOfSymbol(targetProp), targetIsOptional), reportErrors);
13484+
}
13485+
result &= related;
13486+
}
13487+
else {
13488+
if (related) {
13489+
return related;
13490+
}
13491+
}
13492+
}
13493+
if (unionParent && !result && targetIsOptional) {
13494+
result = isRelatedTo(source, undefinedType);
13495+
}
13496+
if (unionParent && !result && reportErrors) {
13497+
// The easiest way to get the right errors here is to un-defer (which may be costly)
13498+
// If it turns out this is too costly too often, we can replicate the error handling logic within
13499+
// typeRelatedToSomeType without the discriminatable type branch (as that requires a manifest union
13500+
// type on which to hand discriminable properties, which we are expressly trying to avoid here)
13501+
return isRelatedTo(source, addOptionality(getTypeOfSymbol(targetProp), targetIsOptional), reportErrors);
13502+
}
13503+
return result;
13504+
}
13505+
else {
13506+
return isRelatedTo(source, addOptionality(getTypeOfSymbol(targetProp), targetIsOptional), reportErrors);
13507+
}
13508+
}
13509+
1344613510
function propertyRelatedTo(source: Type, target: Type, sourceProp: Symbol, targetProp: Symbol, getTypeOfSourceProperty: (sym: Symbol) => Type, reportErrors: boolean): Ternary {
1344713511
const sourcePropFlags = getDeclarationModifierFlagsFromSymbol(sourceProp);
1344813512
const targetPropFlags = getDeclarationModifierFlagsFromSymbol(targetProp);
@@ -13485,7 +13549,7 @@ namespace ts {
1348513549
return Ternary.False;
1348613550
}
1348713551
// If the target comes from a partial union prop, allow `undefined` in the target type
13488-
const related = isRelatedTo(getTypeOfSourceProperty(sourceProp), addOptionality(getTypeOfSymbol(targetProp), !!(getCheckFlags(targetProp) & CheckFlags.Partial)), reportErrors);
13552+
const related = isPropertySymbolTypeRelated(sourceProp, targetProp, getTypeOfSourceProperty, reportErrors);
1348913553
if (!related) {
1349013554
if (reportErrors) {
1349113555
reportError(Diagnostics.Types_of_property_0_are_incompatible, symbolToString(targetProp));
@@ -13597,9 +13661,6 @@ namespace ts {
1359713661
if (!(targetProp.flags & SymbolFlags.Prototype)) {
1359813662
const sourceProp = getPropertyOfType(source, targetProp.escapedName);
1359913663
if (sourceProp && sourceProp !== targetProp) {
13600-
if (isIgnoredJsxProperty(source, sourceProp, getTypeOfSymbol(targetProp))) {
13601-
continue;
13602-
}
1360313664
const related = propertyRelatedTo(source, target, sourceProp, targetProp, getTypeOfSymbol, reportErrors);
1360413665
if (!related) {
1360513666
return Ternary.False;
@@ -13745,7 +13806,7 @@ namespace ts {
1374513806
function eachPropertyRelatedTo(source: Type, target: Type, kind: IndexKind, reportErrors: boolean): Ternary {
1374613807
let result = Ternary.True;
1374713808
for (const prop of getPropertiesOfObjectType(source)) {
13748-
if (isIgnoredJsxProperty(source, prop, /*targetMemberType*/ undefined)) {
13809+
if (isIgnoredJsxProperty(source, prop)) {
1374913810
continue;
1375013811
}
1375113812
// Skip over symbol-named members

src/compiler/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3751,6 +3751,8 @@ namespace ts {
37513751
extendedContainers?: Symbol[]; // Containers (other than the parent) which this symbol is aliased in
37523752
extendedContainersByFile?: Map<Symbol[]>; // Containers (other than the parent) which this symbol is aliased in
37533753
variances?: VarianceFlags[]; // Alias symbol type argument variance cache
3754+
deferralConstituents?: Type[]; // Calculated list of constituents for a deferred type
3755+
deferralParent?: Type; // Source union/intersection of a deferred type
37543756
}
37553757

37563758
/* @internal */
@@ -3777,6 +3779,7 @@ namespace ts {
37773779
ReverseMapped = 1 << 13, // Property of reverse-inferred homomorphic mapped type
37783780
OptionalParameter = 1 << 14, // Optional parameter
37793781
RestParameter = 1 << 15, // Rest parameter
3782+
DeferredType = 1 << 16, // Calculation of the type of this symbol is deferred due to processing costs, should be fetched with `getTypeOfSymbolWithDeferredType`
37803783
Synthetic = SyntheticProperty | SyntheticMethod,
37813784
Discriminant = HasNonUniformType | HasLiteralType,
37823785
Partial = ReadPartial | WritePartial

tests/baselines/reference/intersectionTypeMembers.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@ const de: D & E = {
4343
other: { g: 101 }
4444
}
4545
}
46+
47+
// Additional test case with >2 doubly nested members so fix for #31441 is tested w/ excess props
48+
interface F {
49+
nested: { doublyNested: { g: string; } }
50+
}
51+
52+
interface G {
53+
nested: { doublyNested: { h: string; } }
54+
}
55+
56+
const defg: D & E & F & G = {
57+
nested: {
58+
doublyNested: {
59+
d: 'yes',
60+
f: 'no',
61+
g: 'ok',
62+
h: 'affirmative'
63+
},
64+
different: { e: 12 },
65+
other: { g: 101 }
66+
}
67+
}
4668

4769

4870
//// [intersectionTypeMembers.js]
@@ -69,3 +91,15 @@ var de = {
6991
other: { g: 101 }
7092
}
7193
};
94+
var defg = {
95+
nested: {
96+
doublyNested: {
97+
d: 'yes',
98+
f: 'no',
99+
g: 'ok',
100+
h: 'affirmative'
101+
},
102+
different: { e: 12 },
103+
other: { g: 101 }
104+
}
105+
};

tests/baselines/reference/intersectionTypeMembers.symbols

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,58 @@ const de: D & E = {
146146
}
147147
}
148148

149+
// Additional test case with >2 doubly nested members so fix for #31441 is tested w/ excess props
150+
interface F {
151+
>F : Symbol(F, Decl(intersectionTypeMembers.ts, 43, 1))
152+
153+
nested: { doublyNested: { g: string; } }
154+
>nested : Symbol(F.nested, Decl(intersectionTypeMembers.ts, 46, 13))
155+
>doublyNested : Symbol(doublyNested, Decl(intersectionTypeMembers.ts, 47, 13))
156+
>g : Symbol(g, Decl(intersectionTypeMembers.ts, 47, 29))
157+
}
158+
159+
interface G {
160+
>G : Symbol(G, Decl(intersectionTypeMembers.ts, 48, 1))
161+
162+
nested: { doublyNested: { h: string; } }
163+
>nested : Symbol(G.nested, Decl(intersectionTypeMembers.ts, 50, 13))
164+
>doublyNested : Symbol(doublyNested, Decl(intersectionTypeMembers.ts, 51, 13))
165+
>h : Symbol(h, Decl(intersectionTypeMembers.ts, 51, 29))
166+
}
167+
168+
const defg: D & E & F & G = {
169+
>defg : Symbol(defg, Decl(intersectionTypeMembers.ts, 54, 5))
170+
>D : Symbol(D, Decl(intersectionTypeMembers.ts, 26, 14))
171+
>E : Symbol(E, Decl(intersectionTypeMembers.ts, 30, 1))
172+
>F : Symbol(F, Decl(intersectionTypeMembers.ts, 43, 1))
173+
>G : Symbol(G, Decl(intersectionTypeMembers.ts, 48, 1))
174+
175+
nested: {
176+
>nested : Symbol(nested, Decl(intersectionTypeMembers.ts, 54, 29))
177+
178+
doublyNested: {
179+
>doublyNested : Symbol(doublyNested, Decl(intersectionTypeMembers.ts, 55, 13))
180+
181+
d: 'yes',
182+
>d : Symbol(d, Decl(intersectionTypeMembers.ts, 56, 23))
183+
184+
f: 'no',
185+
>f : Symbol(f, Decl(intersectionTypeMembers.ts, 57, 21))
186+
187+
g: 'ok',
188+
>g : Symbol(g, Decl(intersectionTypeMembers.ts, 58, 20))
189+
190+
h: 'affirmative'
191+
>h : Symbol(h, Decl(intersectionTypeMembers.ts, 59, 20))
192+
193+
},
194+
different: { e: 12 },
195+
>different : Symbol(different, Decl(intersectionTypeMembers.ts, 61, 10))
196+
>e : Symbol(e, Decl(intersectionTypeMembers.ts, 62, 20))
197+
198+
other: { g: 101 }
199+
>other : Symbol(other, Decl(intersectionTypeMembers.ts, 62, 29))
200+
>g : Symbol(g, Decl(intersectionTypeMembers.ts, 63, 16))
201+
}
202+
}
203+

tests/baselines/reference/intersectionTypeMembers.types

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,61 @@ const de: D & E = {
148148
}
149149
}
150150

151+
// Additional test case with >2 doubly nested members so fix for #31441 is tested w/ excess props
152+
interface F {
153+
nested: { doublyNested: { g: string; } }
154+
>nested : { doublyNested: { g: string; }; }
155+
>doublyNested : { g: string; }
156+
>g : string
157+
}
158+
159+
interface G {
160+
nested: { doublyNested: { h: string; } }
161+
>nested : { doublyNested: { h: string; }; }
162+
>doublyNested : { h: string; }
163+
>h : string
164+
}
165+
166+
const defg: D & E & F & G = {
167+
>defg : D & E & F & G
168+
>{ nested: { doublyNested: { d: 'yes', f: 'no', g: 'ok', h: 'affirmative' }, different: { e: 12 }, other: { g: 101 } }} : { nested: { doublyNested: { d: string; f: string; g: string; h: string; }; different: { e: number; }; other: { g: number; }; }; }
169+
170+
nested: {
171+
>nested : { doublyNested: { d: string; f: string; g: string; h: string; }; different: { e: number; }; other: { g: number; }; }
172+
>{ doublyNested: { d: 'yes', f: 'no', g: 'ok', h: 'affirmative' }, different: { e: 12 }, other: { g: 101 } } : { doublyNested: { d: string; f: string; g: string; h: string; }; different: { e: number; }; other: { g: number; }; }
173+
174+
doublyNested: {
175+
>doublyNested : { d: string; f: string; g: string; h: string; }
176+
>{ d: 'yes', f: 'no', g: 'ok', h: 'affirmative' } : { d: string; f: string; g: string; h: string; }
177+
178+
d: 'yes',
179+
>d : string
180+
>'yes' : "yes"
181+
182+
f: 'no',
183+
>f : string
184+
>'no' : "no"
185+
186+
g: 'ok',
187+
>g : string
188+
>'ok' : "ok"
189+
190+
h: 'affirmative'
191+
>h : string
192+
>'affirmative' : "affirmative"
193+
194+
},
195+
different: { e: 12 },
196+
>different : { e: number; }
197+
>{ e: 12 } : { e: number; }
198+
>e : number
199+
>12 : 12
200+
201+
other: { g: 101 }
202+
>other : { g: number; }
203+
>{ g: 101 } : { g: number; }
204+
>g : number
205+
>101 : 101
206+
}
207+
}
208+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//// [reactTagNameComponentWithPropsNoOOM.tsx]
2+
/// <reference path="/.lib/react16.d.ts" />
3+
4+
import * as React from "react";
5+
declare const Tag: keyof React.ReactHTML;
6+
7+
const classes = "";
8+
const rest: {} = {};
9+
const children: any[] = [];
10+
<Tag className={classes} {...rest}>
11+
{children}
12+
</Tag>
13+
14+
//// [reactTagNameComponentWithPropsNoOOM.js]
15+
"use strict";
16+
/// <reference path="react16.d.ts" />
17+
var __assign = (this && this.__assign) || function () {
18+
__assign = Object.assign || function(t) {
19+
for (var s, i = 1, n = arguments.length; i < n; i++) {
20+
s = arguments[i];
21+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22+
t[p] = s[p];
23+
}
24+
return t;
25+
};
26+
return __assign.apply(this, arguments);
27+
};
28+
exports.__esModule = true;
29+
var React = require("react");
30+
var classes = "";
31+
var rest = {};
32+
var children = [];
33+
React.createElement(Tag, __assign({ className: classes }, rest), children);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
=== tests/cases/compiler/reactTagNameComponentWithPropsNoOOM.tsx ===
2+
/// <reference path="react16.d.ts" />
3+
4+
import * as React from "react";
5+
>React : Symbol(React, Decl(reactTagNameComponentWithPropsNoOOM.tsx, 2, 6))
6+
7+
declare const Tag: keyof React.ReactHTML;
8+
>Tag : Symbol(Tag, Decl(reactTagNameComponentWithPropsNoOOM.tsx, 3, 13))
9+
>React : Symbol(React, Decl(reactTagNameComponentWithPropsNoOOM.tsx, 2, 6))
10+
>ReactHTML : Symbol(React.ReactHTML, Decl(react16.d.ts, 2089, 9))
11+
12+
const classes = "";
13+
>classes : Symbol(classes, Decl(reactTagNameComponentWithPropsNoOOM.tsx, 5, 5))
14+
15+
const rest: {} = {};
16+
>rest : Symbol(rest, Decl(reactTagNameComponentWithPropsNoOOM.tsx, 6, 5))
17+
18+
const children: any[] = [];
19+
>children : Symbol(children, Decl(reactTagNameComponentWithPropsNoOOM.tsx, 7, 5))
20+
21+
<Tag className={classes} {...rest}>
22+
>Tag : Symbol(Tag, Decl(reactTagNameComponentWithPropsNoOOM.tsx, 3, 13))
23+
>className : Symbol(className, Decl(reactTagNameComponentWithPropsNoOOM.tsx, 8, 4))
24+
>classes : Symbol(classes, Decl(reactTagNameComponentWithPropsNoOOM.tsx, 5, 5))
25+
>rest : Symbol(rest, Decl(reactTagNameComponentWithPropsNoOOM.tsx, 6, 5))
26+
27+
{children}
28+
>children : Symbol(children, Decl(reactTagNameComponentWithPropsNoOOM.tsx, 7, 5))
29+
30+
</Tag>
31+
>Tag : Symbol(Tag, Decl(reactTagNameComponentWithPropsNoOOM.tsx, 3, 13))
32+

0 commit comments

Comments
 (0)