Skip to content

Commit cd0d3ba

Browse files
committed
Object literals can only have properties that exist in contextual type
1 parent 1344b14 commit cd0d3ba

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

src/compiler/checker.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6830,6 +6830,18 @@ namespace ts {
68306830
return links.resolvedType;
68316831
}
68326832

6833+
function isPermittedProperty(contextualType: Type, propName: string): boolean {
6834+
if (contextualType.flags & TypeFlags.ObjectType) {
6835+
let resolved = resolveStructuredTypeMembers(<ObjectType>contextualType);
6836+
return !!(resolved.properties.length === 0 || resolved.stringIndexType ||
6837+
resolved.numberIndexType || getPropertyOfObjectType(contextualType, propName));
6838+
}
6839+
if (contextualType.flags & TypeFlags.UnionOrIntersection) {
6840+
return !forEach((<UnionOrIntersectionType>contextualType).types, type => !isPermittedProperty(type, propName));
6841+
}
6842+
return true;
6843+
}
6844+
68336845
function checkObjectLiteral(node: ObjectLiteralExpression, contextualMapper?: TypeMapper): Type {
68346846
// Grammar checking
68356847
checkGrammarObjectLiteralExpression(node);
@@ -6879,6 +6891,9 @@ namespace ts {
68796891

68806892
if (!hasDynamicName(memberDecl)) {
68816893
propertiesTable[member.name] = member;
6894+
if (contextualType && !isPermittedProperty(contextualType, member.name)) {
6895+
error(memberDecl.name, Diagnostics.Property_0_does_not_exist_in_contextual_type_1, member.name, typeToString(contextualType));
6896+
}
68826897
}
68836898
propertiesArray.push(member);
68846899
}

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ namespace ts {
414414
The_arguments_object_cannot_be_referenced_in_an_async_arrow_function_Consider_using_a_standard_async_function_expression: { code: 2522, category: DiagnosticCategory.Error, key: "The 'arguments' object cannot be referenced in an async arrow function. Consider using a standard async function expression." },
415415
yield_expressions_cannot_be_used_in_a_parameter_initializer: { code: 2523, category: DiagnosticCategory.Error, key: "'yield' expressions cannot be used in a parameter initializer." },
416416
await_expressions_cannot_be_used_in_a_parameter_initializer: { code: 2524, category: DiagnosticCategory.Error, key: "'await' expressions cannot be used in a parameter initializer." },
417+
Property_0_does_not_exist_in_contextual_type_1: { code: 2525, category: DiagnosticCategory.Error, key: "Property '{0}' does not exist in contextual type '{1}'." },
417418
JSX_element_attributes_type_0_must_be_an_object_type: { code: 2600, category: DiagnosticCategory.Error, key: "JSX element attributes type '{0}' must be an object type." },
418419
The_return_type_of_a_JSX_element_constructor_must_return_an_object_type: { code: 2601, category: DiagnosticCategory.Error, key: "The return type of a JSX element constructor must return an object type." },
419420
JSX_element_implicitly_has_type_any_because_the_global_type_JSX_Element_does_not_exist: { code: 2602, category: DiagnosticCategory.Error, key: "JSX element implicitly has type 'any' because the global type 'JSX.Element' does not exist." },

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,10 @@
16451645
"category": "Error",
16461646
"code": 2524
16471647
},
1648+
"Property '{0}' does not exist in contextual type '{1}'.": {
1649+
"category": "Error",
1650+
"code": 2525
1651+
},
16481652
"JSX element attributes type '{0}' must be an object type.": {
16491653
"category": "Error",
16501654
"code": 2600

0 commit comments

Comments
 (0)