Skip to content

Commit e6390ef

Browse files
authored
Properly handle private/protected members in unions of object types (microsoft#38277)
* Property handle private/protected properties in unions of object types * Add regression test
1 parent d9c9c9d commit e6390ef

File tree

5 files changed

+95
-3
lines changed

5 files changed

+95
-3
lines changed

src/compiler/checker.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -10539,7 +10539,6 @@ namespace ts {
1053910539
let propSet: Map<Symbol> | undefined;
1054010540
let indexTypes: Type[] | undefined;
1054110541
const isUnion = containingType.flags & TypeFlags.Union;
10542-
const excludeModifiers = isUnion ? ModifierFlags.NonPublicAccessibilityModifier : 0;
1054310542
// Flags we want to propagate to the result if they exist in all source symbols
1054410543
let optionalFlag = isUnion ? SymbolFlags.None : SymbolFlags.Optional;
1054510544
let syntheticFlag = CheckFlags.SyntheticMethod;
@@ -10549,7 +10548,7 @@ namespace ts {
1054910548
if (!(type === errorType || type.flags & TypeFlags.Never)) {
1055010549
const prop = getPropertyOfType(type, name);
1055110550
const modifiers = prop ? getDeclarationModifierFlagsFromSymbol(prop) : 0;
10552-
if (prop && !(modifiers & excludeModifiers)) {
10551+
if (prop) {
1055310552
if (isUnion) {
1055410553
optionalFlag |= (prop.flags & SymbolFlags.Optional);
1055510554
}
@@ -10594,7 +10593,9 @@ namespace ts {
1059410593
}
1059510594
}
1059610595
}
10597-
if (!singleProp) {
10596+
if (!singleProp || isUnion && (propSet || checkFlags & CheckFlags.Partial) && checkFlags & (CheckFlags.ContainsPrivate | CheckFlags.ContainsProtected)) {
10597+
// No property was found, or, in a union, a property has a private or protected declaration in one
10598+
// constituent, but is missing or has a different declaration in another constituent.
1059810599
return undefined;
1059910600
}
1060010601
if (!propSet && !(checkFlags & CheckFlags.ReadPartial) && !indexTypes) {
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+
}());
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+
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+
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)