Fix type of computed name following spread (#39319)

This commit is contained in:
Wesley Wigham 2020-06-30 13:37:59 -07:00 committed by GitHub
parent 8b6a88700e
commit ff1f449b99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 4 deletions

View File

@ -23836,10 +23836,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]));
}
}
@ -23892,8 +23897,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;
@ -23980,7 +23984,7 @@ namespace ts {
checkSpreadPropOverrides(type, allPropertiesTable, memberDecl);
}
spread = getSpreadType(spread, type, node.symbol, objectFlags, inConstContext);
offset = i + 1;
offset = propertiesArray.length;
continue;
}
else {

View File

@ -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));

View File

@ -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))

View File

@ -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"
>"" : ""

View File

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