Skip to content

Commit d548f6a

Browse files
committed
Consider object literals in unions to have properties of type 'undefined'
1 parent 0895fd6 commit d548f6a

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8016,10 +8016,13 @@ namespace ts {
80168016
else if (isUnion) {
80178017
const indexInfo = !isLateBoundName(name) && (isNumericLiteralName(name) && getIndexInfoOfType(type, IndexKind.Number) || getIndexInfoOfType(type, IndexKind.String));
80188018
if (indexInfo) {
8019-
checkFlags |= indexInfo.isReadonly ? CheckFlags.Readonly : 0;
8020-
checkFlags |= CheckFlags.WritePartial;
8019+
checkFlags |= CheckFlags.WritePartial | (indexInfo.isReadonly ? CheckFlags.Readonly : 0);
80218020
indexTypes = append(indexTypes, isTupleType(type) ? getRestTypeOfTupleType(type) || undefinedType : indexInfo.type);
80228021
}
8022+
else if (isObjectLiteralType(type)) {
8023+
checkFlags |= CheckFlags.WritePartial;
8024+
indexTypes = append(indexTypes, undefinedType);
8025+
}
80238026
else {
80248027
checkFlags |= CheckFlags.ReadPartial;
80258028
}
@@ -20181,7 +20184,8 @@ namespace ts {
2018120184
let propType: Type;
2018220185
const leftType = checkNonNullExpression(left);
2018320186
const parentSymbol = getNodeLinks(left).resolvedSymbol;
20184-
const apparentType = getApparentType(getWidenedType(leftType));
20187+
// We widen array literals to get type any[] instead of undefined[] in non-strict mode
20188+
const apparentType = getApparentType(isEmptyArrayLiteralType(leftType) ? getWidenedType(leftType) : leftType);
2018520189
if (isTypeAny(apparentType) || apparentType === silentNeverType) {
2018620190
if (isIdentifier(left) && parentSymbol) {
2018720191
markAliasReferenced(parentSymbol, node);

0 commit comments

Comments
 (0)