Skip to content

Commit bd5ce28

Browse files
committed
Set spread type symbols in checkObjectLiteral
Instead of getSpreadType. Also clean up special-case handling inside getSpreadType to be more readable.
1 parent 567f563 commit bd5ce28

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5881,7 +5881,7 @@ namespace ts {
58815881
* this function should be called in a left folding style, with left = previous result of getSpreadType
58825882
* and right = the new element to be spread.
58835883
*/
5884-
function getSpreadType(left: Type, right: Type, symbol: Symbol): ResolvedType | IntrinsicType {
5884+
function getSpreadType(left: Type, right: Type, isFromObjectLiteral: boolean): ResolvedType | IntrinsicType {
58855885
Debug.assert(!!(left.flags & (TypeFlags.Object | TypeFlags.Any)) && !!(right.flags & (TypeFlags.Object | TypeFlags.Any)), "Only object types may be spread.");
58865886
if (left.flags & TypeFlags.Any || right.flags & TypeFlags.Any) {
58875887
return anyType;
@@ -5900,14 +5900,14 @@ namespace ts {
59005900
numberIndexInfo = unionSpreadIndexInfos(getIndexInfoOfType(left, IndexKind.Number), getIndexInfoOfType(right, IndexKind.Number));
59015901
}
59025902

5903-
const isFromSpread = right.symbol !== symbol;
59045903
for (const rightProp of getPropertiesOfType(right)) {
5904+
// we approximate own properties as non-methods plus methods that are inside the object literal
5905+
const isOwnProperty = !(rightProp.flags & SymbolFlags.Method) || isFromObjectLiteral;
5906+
const isSetterWithoutGetter = rightProp.flags & SymbolFlags.SetAccessor && !(rightProp.flags & SymbolFlags.GetAccessor);
59055907
if (getDeclarationModifierFlagsFromSymbol(rightProp) & (ModifierFlags.Private | ModifierFlags.Protected)) {
59065908
skippedPrivateMembers[rightProp.name] = true;
59075909
}
5908-
else if (!(rightProp.flags & SymbolFlags.Method && isFromSpread) &&
5909-
!(rightProp.flags & SymbolFlags.SetAccessor && !(rightProp.flags & SymbolFlags.GetAccessor))) {
5910-
// skip methods from spreads and accessors with setters but no getters
5910+
else if (isOwnProperty && !isSetterWithoutGetter) {
59115911
members[rightProp.name] = rightProp;
59125912
}
59135913
}
@@ -5937,7 +5937,7 @@ namespace ts {
59375937
members[leftProp.name] = leftProp;
59385938
}
59395939
}
5940-
return createAnonymousType(symbol, members, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo);
5940+
return createAnonymousType(undefined, members, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo);
59415941
}
59425942

59435943
function createLiteralType(flags: TypeFlags, text: string) {
@@ -10947,7 +10947,7 @@ namespace ts {
1094710947
}
1094810948
else if (memberDecl.kind === SyntaxKind.SpreadAssignment) {
1094910949
if (propertiesArray.length > 0) {
10950-
spread = getSpreadType(spread, createObjectLiteralType(), node.symbol);
10950+
spread = getSpreadType(spread, createObjectLiteralType(), /*isFromObjectLiteral*/ true);
1095110951
propertiesArray = [];
1095210952
propertiesTable = createMap<Symbol>();
1095310953
hasComputedStringProperty = false;
@@ -10959,7 +10959,7 @@ namespace ts {
1095910959
error(memberDecl, Diagnostics.Spread_types_may_only_be_created_from_object_types);
1096010960
return unknownType;
1096110961
}
10962-
spread = getSpreadType(spread, type, node.symbol);
10962+
spread = getSpreadType(spread, type, /*isFromObjectLiteral*/ false);
1096310963
continue;
1096410964
}
1096510965
else {
@@ -11003,9 +11003,10 @@ namespace ts {
1100311003

1100411004
if (spread !== emptyObjectType) {
1100511005
if (propertiesArray.length > 0) {
11006-
spread = getSpreadType(spread, createObjectLiteralType(), node.symbol);
11006+
spread = getSpreadType(spread, createObjectLiteralType(), /*isFromObjectLiteral*/ true);
1100711007
}
1100811008
spread.flags |= propagatedFlags;
11009+
spread.symbol = node.symbol;
1100911010
return spread;
1101011011
}
1101111012

tests/baselines/reference/objectSpread.symbols

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ let cplus: { p: number, plus(): void } = { ...c, plus() { return this.p + 1; } }
182182
>p : Symbol(p, Decl(objectSpread.ts, 49, 12))
183183
>plus : Symbol(plus, Decl(objectSpread.ts, 49, 23))
184184
>plus : Symbol(plus, Decl(objectSpread.ts, 49, 48))
185+
>this : Symbol(__object, Decl(objectSpread.ts, 41, 15))
185186

186187
cplus.plus();
187188
>cplus.plus : Symbol(plus, Decl(objectSpread.ts, 49, 23))

0 commit comments

Comments
 (0)