Skip to content

Fix type of computed name following spread #39319

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
12 changes: 8 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23824,10 +23824,15 @@ namespace ts {
return links.resolvedType;
}

function isSymbolWithNumericName(symbol: Symbol) {
const firstDecl = symbol.declarations?.[0];
return isNumericLiteralName(symbol.escapedName) || (firstDecl && isNamedDeclaration(firstDecl) && isNumericName(firstDecl.name));
}

function getObjectLiteralIndexInfo(node: ObjectLiteralExpression, offset: number, properties: Symbol[], kind: IndexKind): IndexInfo {
const propTypes: Type[] = [];
for (let i = offset; i < properties.length; i++) {
if (kind === IndexKind.String || isNumericName(node.properties[i].name!)) {
if (kind === IndexKind.String || isSymbolWithNumericName(properties[i])) {
propTypes.push(getTypeOfSymbol(properties[i]));
}
}
Expand Down Expand Up @@ -23880,8 +23885,7 @@ namespace ts {
}

let offset = 0;
for (let i = 0; i < node.properties.length; i++) {
const memberDecl = node.properties[i];
for (const memberDecl of node.properties) {
let member = getSymbolOfNode(memberDecl);
const computedNameType = memberDecl.name && memberDecl.name.kind === SyntaxKind.ComputedPropertyName && !isWellKnownSymbolSyntactically(memberDecl.name.expression) ?
checkComputedPropertyName(memberDecl.name) : undefined;
Expand Down Expand Up @@ -23965,7 +23969,7 @@ namespace ts {
checkSpreadPropOverrides(type, allPropertiesTable, memberDecl);
}
spread = getSpreadType(spread, type, node.symbol, objectFlags, inConstContext);
offset = i + 1;
offset = propertiesArray.length;
continue;
}
else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//// [spreadObjectWithIndexDoesNotAddUndefinedToLocalIndex.ts]
declare const m: { [k: string]: string };
const x: { [k: string]: string } = { ...m, ["a" + "b"]: "" };

//// [spreadObjectWithIndexDoesNotAddUndefinedToLocalIndex.js]
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var _a;
var x = __assign(__assign({}, m), (_a = {}, _a["a" + "b"] = "", _a));
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
=== tests/cases/compiler/spreadObjectWithIndexDoesNotAddUndefinedToLocalIndex.ts ===
declare const m: { [k: string]: string };
>m : Symbol(m, Decl(spreadObjectWithIndexDoesNotAddUndefinedToLocalIndex.ts, 0, 13))
>k : Symbol(k, Decl(spreadObjectWithIndexDoesNotAddUndefinedToLocalIndex.ts, 0, 20))

const x: { [k: string]: string } = { ...m, ["a" + "b"]: "" };
>x : Symbol(x, Decl(spreadObjectWithIndexDoesNotAddUndefinedToLocalIndex.ts, 1, 5))
>k : Symbol(k, Decl(spreadObjectWithIndexDoesNotAddUndefinedToLocalIndex.ts, 1, 12))
>m : Symbol(m, Decl(spreadObjectWithIndexDoesNotAddUndefinedToLocalIndex.ts, 0, 13))
>["a" + "b"] : Symbol(["a" + "b"], Decl(spreadObjectWithIndexDoesNotAddUndefinedToLocalIndex.ts, 1, 42))

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/compiler/spreadObjectWithIndexDoesNotAddUndefinedToLocalIndex.ts ===
declare const m: { [k: string]: string };
>m : { [k: string]: string; }
>k : string

const x: { [k: string]: string } = { ...m, ["a" + "b"]: "" };
>x : { [k: string]: string; }
>k : string
>{ ...m, ["a" + "b"]: "" } : { [x: string]: string; }
>m : { [k: string]: string; }
>["a" + "b"] : string
>"a" + "b" : string
>"a" : "a"
>"b" : "b"
>"" : ""

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @strict: true
declare const m: { [k: string]: string };
const x: { [k: string]: string } = { ...m, ["a" + "b"]: "" };