Consider object literals in unions to have properties of type 'undefined'

This commit is contained in:
Anders Hejlsberg
2019-06-01 09:45:58 -07:00
parent 0895fd6bdc
commit d548f6af70

View File

@@ -8016,10 +8016,13 @@ namespace ts {
else if (isUnion) {
const indexInfo = !isLateBoundName(name) && (isNumericLiteralName(name) && getIndexInfoOfType(type, IndexKind.Number) || getIndexInfoOfType(type, IndexKind.String));
if (indexInfo) {
checkFlags |= indexInfo.isReadonly ? CheckFlags.Readonly : 0;
checkFlags |= CheckFlags.WritePartial;
checkFlags |= CheckFlags.WritePartial | (indexInfo.isReadonly ? CheckFlags.Readonly : 0);
indexTypes = append(indexTypes, isTupleType(type) ? getRestTypeOfTupleType(type) || undefinedType : indexInfo.type);
}
else if (isObjectLiteralType(type)) {
checkFlags |= CheckFlags.WritePartial;
indexTypes = append(indexTypes, undefinedType);
}
else {
checkFlags |= CheckFlags.ReadPartial;
}
@@ -20181,7 +20184,8 @@ namespace ts {
let propType: Type;
const leftType = checkNonNullExpression(left);
const parentSymbol = getNodeLinks(left).resolvedSymbol;
const apparentType = getApparentType(getWidenedType(leftType));
// We widen array literals to get type any[] instead of undefined[] in non-strict mode
const apparentType = getApparentType(isEmptyArrayLiteralType(leftType) ? getWidenedType(leftType) : leftType);
if (isTypeAny(apparentType) || apparentType === silentNeverType) {
if (isIdentifier(left) && parentSymbol) {
markAliasReferenced(parentSymbol, node);