Skip to content

Commit b7d3459

Browse files
Properly handle private/protected members in unions of object types (#38277) (#38334)
* Property handle private/protected properties in unions of object types * Add regression test Co-authored-by: Anders Hejlsberg <[email protected]>
1 parent e261cdd commit b7d3459

File tree

5 files changed

+112
-10
lines changed

5 files changed

+112
-10
lines changed

src/compiler/checker.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10477,10 +10477,10 @@ namespace ts {
1047710477
}
1047810478

1047910479
function createUnionOrIntersectionProperty(containingType: UnionOrIntersectionType, name: __String): Symbol | undefined {
10480-
const propSet = createMap<Symbol>();
10480+
let singleProp: Symbol | undefined;
10481+
let propSet: Map<Symbol> | undefined;
1048110482
let indexTypes: Type[] | undefined;
1048210483
const isUnion = containingType.flags & TypeFlags.Union;
10483-
const excludeModifiers = isUnion ? ModifierFlags.NonPublicAccessibilityModifier : 0;
1048410484
// Flags we want to propagate to the result if they exist in all source symbols
1048510485
let optionalFlag = isUnion ? SymbolFlags.None : SymbolFlags.Optional;
1048610486
let syntheticFlag = CheckFlags.SyntheticMethod;
@@ -10490,16 +10490,25 @@ namespace ts {
1049010490
if (!(type === errorType || type.flags & TypeFlags.Never)) {
1049110491
const prop = getPropertyOfType(type, name);
1049210492
const modifiers = prop ? getDeclarationModifierFlagsFromSymbol(prop) : 0;
10493-
if (prop && !(modifiers & excludeModifiers)) {
10493+
if (prop) {
1049410494
if (isUnion) {
1049510495
optionalFlag |= (prop.flags & SymbolFlags.Optional);
1049610496
}
1049710497
else {
1049810498
optionalFlag &= prop.flags;
1049910499
}
10500-
const id = "" + getSymbolId(prop);
10501-
if (!propSet.has(id)) {
10502-
propSet.set(id, prop);
10500+
if (!singleProp) {
10501+
singleProp = prop;
10502+
}
10503+
else if (prop !== singleProp) {
10504+
if (!propSet) {
10505+
propSet = createMap<Symbol>();
10506+
propSet.set("" + getSymbolId(singleProp), singleProp);
10507+
}
10508+
const id = "" + getSymbolId(prop);
10509+
if (!propSet.has(id)) {
10510+
propSet.set(id, prop);
10511+
}
1050310512
}
1050410513
checkFlags |= (isReadonlySymbol(prop) ? CheckFlags.Readonly : 0) |
1050510514
(!(modifiers & ModifierFlags.NonPublicAccessibilityModifier) ? CheckFlags.ContainsPublic : 0) |
@@ -10526,13 +10535,15 @@ namespace ts {
1052610535
}
1052710536
}
1052810537
}
10529-
if (!propSet.size) {
10538+
if (!singleProp || isUnion && (propSet || checkFlags & CheckFlags.Partial) && checkFlags & (CheckFlags.ContainsPrivate | CheckFlags.ContainsProtected)) {
10539+
// No property was found, or, in a union, a property has a private or protected declaration in one
10540+
// constituent, but is missing or has a different declaration in another constituent.
1053010541
return undefined;
1053110542
}
10532-
const props = arrayFrom(propSet.values());
10533-
if (props.length === 1 && !(checkFlags & CheckFlags.ReadPartial) && !indexTypes) {
10534-
return props[0];
10543+
if (!propSet && !(checkFlags & CheckFlags.ReadPartial) && !indexTypes) {
10544+
return singleProp;
1053510545
}
10546+
const props = propSet ? arrayFrom(propSet.values()) : [singleProp];
1053610547
let declarations: Declaration[] | undefined;
1053710548
let firstType: Type | undefined;
1053810549
let nameType: Type | undefined;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//// [privatePropertyInUnion.ts]
2+
// Repro from #38236
3+
4+
type Type = string | object;
5+
6+
class SyncableObject {
7+
private foo: unknown;
8+
}
9+
10+
interface SyncableRef<T extends ISyncableObject> {}
11+
12+
interface ISyncableObject<T = object> extends SyncableObject {}
13+
14+
type __ValueDescriptorType<T extends string | object> = T extends ISyncableObject ? SyncableRef<T> : T;
15+
16+
17+
//// [privatePropertyInUnion.js]
18+
"use strict";
19+
// Repro from #38236
20+
var SyncableObject = /** @class */ (function () {
21+
function SyncableObject() {
22+
}
23+
return SyncableObject;
24+
}());
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
=== tests/cases/compiler/privatePropertyInUnion.ts ===
2+
// Repro from #38236
3+
4+
type Type = string | object;
5+
>Type : Symbol(Type, Decl(privatePropertyInUnion.ts, 0, 0))
6+
7+
class SyncableObject {
8+
>SyncableObject : Symbol(SyncableObject, Decl(privatePropertyInUnion.ts, 2, 28))
9+
10+
private foo: unknown;
11+
>foo : Symbol(SyncableObject.foo, Decl(privatePropertyInUnion.ts, 4, 22))
12+
}
13+
14+
interface SyncableRef<T extends ISyncableObject> {}
15+
>SyncableRef : Symbol(SyncableRef, Decl(privatePropertyInUnion.ts, 6, 1))
16+
>T : Symbol(T, Decl(privatePropertyInUnion.ts, 8, 22))
17+
>ISyncableObject : Symbol(ISyncableObject, Decl(privatePropertyInUnion.ts, 8, 51))
18+
19+
interface ISyncableObject<T = object> extends SyncableObject {}
20+
>ISyncableObject : Symbol(ISyncableObject, Decl(privatePropertyInUnion.ts, 8, 51))
21+
>T : Symbol(T, Decl(privatePropertyInUnion.ts, 10, 26))
22+
>SyncableObject : Symbol(SyncableObject, Decl(privatePropertyInUnion.ts, 2, 28))
23+
24+
type __ValueDescriptorType<T extends string | object> = T extends ISyncableObject ? SyncableRef<T> : T;
25+
>__ValueDescriptorType : Symbol(__ValueDescriptorType, Decl(privatePropertyInUnion.ts, 10, 63))
26+
>T : Symbol(T, Decl(privatePropertyInUnion.ts, 12, 27))
27+
>T : Symbol(T, Decl(privatePropertyInUnion.ts, 12, 27))
28+
>ISyncableObject : Symbol(ISyncableObject, Decl(privatePropertyInUnion.ts, 8, 51))
29+
>SyncableRef : Symbol(SyncableRef, Decl(privatePropertyInUnion.ts, 6, 1))
30+
>T : Symbol(T, Decl(privatePropertyInUnion.ts, 12, 27))
31+
>T : Symbol(T, Decl(privatePropertyInUnion.ts, 12, 27))
32+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== tests/cases/compiler/privatePropertyInUnion.ts ===
2+
// Repro from #38236
3+
4+
type Type = string | object;
5+
>Type : string | object
6+
7+
class SyncableObject {
8+
>SyncableObject : SyncableObject
9+
10+
private foo: unknown;
11+
>foo : unknown
12+
}
13+
14+
interface SyncableRef<T extends ISyncableObject> {}
15+
16+
interface ISyncableObject<T = object> extends SyncableObject {}
17+
18+
type __ValueDescriptorType<T extends string | object> = T extends ISyncableObject ? SyncableRef<T> : T;
19+
>__ValueDescriptorType : __ValueDescriptorType<T>
20+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// @strict: true
2+
3+
// Repro from #38236
4+
5+
type Type = string | object;
6+
7+
class SyncableObject {
8+
private foo: unknown;
9+
}
10+
11+
interface SyncableRef<T extends ISyncableObject> {}
12+
13+
interface ISyncableObject<T = object> extends SyncableObject {}
14+
15+
type __ValueDescriptorType<T extends string | object> = T extends ISyncableObject ? SyncableRef<T> : T;

0 commit comments

Comments
 (0)