Skip to content

Commit 07dbc56

Browse files
authored
Merge pull request #28854 from Microsoft/fixExcessPropertyChecks
Improve excess property checks
2 parents 2109c34 + 6e022bd commit 07dbc56

8 files changed

+232
-25
lines changed

src/compiler/checker.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11835,7 +11835,7 @@ namespace ts {
1183511835
if (!noImplicitAny && getObjectFlags(target) & ObjectFlags.JSLiteral) {
1183611836
return false; // Disable excess property checks on JS literals to simulate having an implicit "index signature" - but only outside of noImplicitAny
1183711837
}
11838-
if (maybeTypeOfKind(target, TypeFlags.Object) && !(getObjectFlags(target) & ObjectFlags.ObjectLiteralPatternWithComputedProperties)) {
11838+
if (isExcessPropertyCheckTarget(target)) {
1183911839
const isComparingJsxAttributes = !!(getObjectFlags(source) & ObjectFlags.JsxAttributes);
1184011840
if ((relation === assignableRelation || relation === definitelyAssignableRelation || relation === comparableRelation) &&
1184111841
(isTypeSubsetOf(globalObjectType, target) || (!isComparingJsxAttributes && isEmptyObjectType(target)))) {
@@ -11848,6 +11848,9 @@ namespace ts {
1184811848
for (const prop of getPropertiesOfObjectType(source)) {
1184911849
if (shouldCheckAsExcessProperty(prop, source.symbol) && !isKnownProperty(target, prop.escapedName, isComparingJsxAttributes)) {
1185011850
if (reportErrors) {
11851+
// Report error in terms of object types in the target as those are the only ones
11852+
// we check in isKnownProperty.
11853+
const errorTarget = filterType(target, isExcessPropertyCheckTarget);
1185111854
// We know *exactly* where things went wrong when comparing the types.
1185211855
// Use this property as the error node as this will be more helpful in
1185311856
// reasoning about what went wrong.
@@ -11856,7 +11859,7 @@ namespace ts {
1185611859
// JsxAttributes has an object-literal flag and undergo same type-assignablity check as normal object-literal.
1185711860
// However, using an object-literal error message will be very confusing to the users so we give different a message.
1185811861
// TODO: Spelling suggestions for excess jsx attributes (needs new diagnostic messages)
11859-
reportError(Diagnostics.Property_0_does_not_exist_on_type_1, symbolToString(prop), typeToString(target));
11862+
reportError(Diagnostics.Property_0_does_not_exist_on_type_1, symbolToString(prop), typeToString(errorTarget));
1186011863
}
1186111864
else {
1186211865
// use the property's value declaration if the property is assigned inside the literal itself
@@ -11870,17 +11873,17 @@ namespace ts {
1187011873

1187111874
const name = propDeclaration.name!;
1187211875
if (isIdentifier(name)) {
11873-
suggestion = getSuggestionForNonexistentProperty(name, target);
11876+
suggestion = getSuggestionForNonexistentProperty(name, errorTarget);
1187411877
}
1187511878
}
1187611879

1187711880
if (suggestion !== undefined) {
1187811881
reportError(Diagnostics.Object_literal_may_only_specify_known_properties_but_0_does_not_exist_in_type_1_Did_you_mean_to_write_2,
11879-
symbolToString(prop), typeToString(target), suggestion);
11882+
symbolToString(prop), typeToString(errorTarget), suggestion);
1188011883
}
1188111884
else {
1188211885
reportError(Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1,
11883-
symbolToString(prop), typeToString(target));
11886+
symbolToString(prop), typeToString(errorTarget));
1188411887
}
1188511888
}
1188611889
}
@@ -18598,20 +18601,23 @@ namespace ts {
1859818601
return true;
1859918602
}
1860018603
}
18601-
else if (targetType.flags & TypeFlags.UnionOrIntersection) {
18604+
else if (targetType.flags & TypeFlags.UnionOrIntersection && isExcessPropertyCheckTarget(targetType)) {
1860218605
for (const t of (targetType as UnionOrIntersectionType).types) {
1860318606
if (isKnownProperty(t, name, isComparingJsxAttributes)) {
1860418607
return true;
1860518608
}
1860618609
}
1860718610
}
18608-
else if (targetType.flags & TypeFlags.Conditional) {
18609-
return isKnownProperty((targetType as ConditionalType).root.trueType, name, isComparingJsxAttributes) ||
18610-
isKnownProperty((targetType as ConditionalType).root.falseType, name, isComparingJsxAttributes);
18611-
}
1861218611
return false;
1861318612
}
1861418613

18614+
function isExcessPropertyCheckTarget(type: Type): boolean {
18615+
return !!(type.flags & TypeFlags.Object && !(getObjectFlags(type) & ObjectFlags.ObjectLiteralPatternWithComputedProperties) ||
18616+
type.flags & TypeFlags.NonPrimitive ||
18617+
type.flags & TypeFlags.Union && some((<UnionType>type).types, isExcessPropertyCheckTarget) ||
18618+
type.flags & TypeFlags.Intersection && every((<IntersectionType>type).types, isExcessPropertyCheckTarget));
18619+
}
18620+
1861518621
function checkJsxExpression(node: JsxExpression, checkMode?: CheckMode) {
1861618622
if (node.expression) {
1861718623
const type = checkExpression(node.expression, checkMode);

tests/baselines/reference/conditionalTypesExcessProperties.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
tests/cases/conformance/types/conditional/conditionalTypesExcessProperties.ts(8,5): error TS2322: Type '{ test: string; arg: A; }' is not assignable to type 'Something<A>'.
22
Type '{ test: string; arg: A; }' is not assignable to type 'A extends object ? { arg: A; } : { arg?: undefined; }'.
3-
tests/cases/conformance/types/conditional/conditionalTypesExcessProperties.ts(9,33): error TS2322: Type '{ test: string; arg: A; arr: A; }' is not assignable to type 'Something<A>'.
4-
Object literal may only specify known properties, and 'arr' does not exist in type 'Something<A>'.
3+
tests/cases/conformance/types/conditional/conditionalTypesExcessProperties.ts(9,5): error TS2322: Type '{ test: string; arg: A; arr: A; }' is not assignable to type 'Something<A>'.
4+
Type '{ test: string; arg: A; arr: A; }' is not assignable to type 'A extends object ? { arg: A; } : { arg?: undefined; }'.
55

66

77
==== tests/cases/conformance/types/conditional/conditionalTypesExcessProperties.ts (2 errors) ====
@@ -17,8 +17,8 @@ tests/cases/conformance/types/conditional/conditionalTypesExcessProperties.ts(9,
1717
!!! error TS2322: Type '{ test: string; arg: A; }' is not assignable to type 'Something<A>'.
1818
!!! error TS2322: Type '{ test: string; arg: A; }' is not assignable to type 'A extends object ? { arg: A; } : { arg?: undefined; }'.
1919
sa = { test: 'bye', arg: a, arr: a } // excess
20-
~~~~~~
20+
~~
2121
!!! error TS2322: Type '{ test: string; arg: A; arr: A; }' is not assignable to type 'Something<A>'.
22-
!!! error TS2322: Object literal may only specify known properties, and 'arr' does not exist in type 'Something<A>'.
22+
!!! error TS2322: Type '{ test: string; arg: A; arr: A; }' is not assignable to type 'A extends object ? { arg: A; } : { arg?: undefined; }'.
2323
}
2424

tests/baselines/reference/objectLiteralExcessProperties.errors.txt

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
tests/cases/compiler/objectLiteralExcessProperties.ts(9,18): error TS2322: Type '{ forword: string; }' is not assignable to type 'Book'.
22
Object literal may only specify known properties, but 'forword' does not exist in type 'Book'. Did you mean to write 'foreword'?
33
tests/cases/compiler/objectLiteralExcessProperties.ts(11,27): error TS2322: Type '{ foreward: string; }' is not assignable to type 'string | Book'.
4-
Object literal may only specify known properties, and 'foreward' does not exist in type 'string | Book'.
4+
Object literal may only specify known properties, but 'foreward' does not exist in type 'Book'. Did you mean to write 'foreword'?
55
tests/cases/compiler/objectLiteralExcessProperties.ts(13,53): error TS2322: Type '({ foreword: string; } | { forwards: string; })[]' is not assignable to type 'Book | Book[]'.
66
Type '({ foreword: string; } | { forwards: string; })[]' is not assignable to type 'Book[]'.
77
Type '{ foreword: string; } | { forwards: string; }' is not assignable to type 'Book'.
@@ -13,17 +13,27 @@ tests/cases/compiler/objectLiteralExcessProperties.ts(17,26): error TS2322: Type
1313
Object literal may only specify known properties, but 'foreward' does not exist in type 'Book & Cover'. Did you mean to write 'foreword'?
1414
tests/cases/compiler/objectLiteralExcessProperties.ts(19,57): error TS2322: Type '{ foreword: string; color: string; price: number; }' is not assignable to type 'Book & Cover'.
1515
Object literal may only specify known properties, and 'price' does not exist in type 'Book & Cover'.
16-
tests/cases/compiler/objectLiteralExcessProperties.ts(21,43): error TS2322: Type '{ foreword: string; price: number; }' is not assignable to type 'Book & number'.
17-
Object literal may only specify known properties, and 'price' does not exist in type 'Book & number'.
16+
tests/cases/compiler/objectLiteralExcessProperties.ts(21,5): error TS2322: Type '{ foreword: string; price: number; }' is not assignable to type 'Book & number'.
17+
Type '{ foreword: string; price: number; }' is not assignable to type 'number'.
1818
tests/cases/compiler/objectLiteralExcessProperties.ts(23,29): error TS2322: Type '{ couleur: string; }' is not assignable to type 'Cover | Cover[]'.
1919
Object literal may only specify known properties, and 'couleur' does not exist in type 'Cover | Cover[]'.
2020
tests/cases/compiler/objectLiteralExcessProperties.ts(25,27): error TS2322: Type '{ forewarned: string; }' is not assignable to type 'Book | Book[]'.
2121
Object literal may only specify known properties, and 'forewarned' does not exist in type 'Book | Book[]'.
2222
tests/cases/compiler/objectLiteralExcessProperties.ts(33,27): error TS2322: Type '{ colour: string; }' is not assignable to type 'Cover'.
2323
Object literal may only specify known properties, but 'colour' does not exist in type 'Cover'. Did you mean to write 'color'?
24+
tests/cases/compiler/objectLiteralExcessProperties.ts(37,25): error TS2304: Cannot find name 'IFoo'.
25+
tests/cases/compiler/objectLiteralExcessProperties.ts(39,11): error TS2322: Type '{ name: string; }' is not assignable to type 'T'.
26+
tests/cases/compiler/objectLiteralExcessProperties.ts(41,11): error TS2322: Type '{ name: string; prop: boolean; }' is not assignable to type 'T & { prop: boolean; }'.
27+
Type '{ name: string; prop: boolean; }' is not assignable to type 'T'.
28+
tests/cases/compiler/objectLiteralExcessProperties.ts(43,43): error TS2322: Type '{ name: string; prop: boolean; }' is not assignable to type 'T | { prop: boolean; }'.
29+
Object literal may only specify known properties, and 'name' does not exist in type '{ prop: boolean; }'.
30+
tests/cases/compiler/objectLiteralExcessProperties.ts(45,76): error TS2322: Type '{ name: string; prop: boolean; }' is not assignable to type '{ name: string; } | (T & { prop: boolean; })'.
31+
Object literal may only specify known properties, and 'prop' does not exist in type '{ name: string; }'.
32+
tests/cases/compiler/objectLiteralExcessProperties.ts(49,44): error TS2322: Type '{ z: string; }' is not assignable to type 'object & { x: string; }'.
33+
Object literal may only specify known properties, and 'z' does not exist in type 'object & { x: string; }'.
2434

2535

26-
==== tests/cases/compiler/objectLiteralExcessProperties.ts (10 errors) ====
36+
==== tests/cases/compiler/objectLiteralExcessProperties.ts (16 errors) ====
2737
interface Book {
2838
foreword: string;
2939
}
@@ -40,7 +50,7 @@ tests/cases/compiler/objectLiteralExcessProperties.ts(33,27): error TS2322: Type
4050
var b2: Book | string = { foreward: "nope" };
4151
~~~~~~~~~~~~~~~~
4252
!!! error TS2322: Type '{ foreward: string; }' is not assignable to type 'string | Book'.
43-
!!! error TS2322: Object literal may only specify known properties, and 'foreward' does not exist in type 'string | Book'.
53+
!!! error TS2322: Object literal may only specify known properties, but 'foreward' does not exist in type 'Book'. Did you mean to write 'foreword'?
4454

4555
var b3: Book | (Book[]) = [{ foreword: "hello" }, { forwards: "back" }];
4656
~~~~~~~~~~~~~~~~
@@ -66,9 +76,9 @@ tests/cases/compiler/objectLiteralExcessProperties.ts(33,27): error TS2322: Type
6676
!!! error TS2322: Object literal may only specify known properties, and 'price' does not exist in type 'Book & Cover'.
6777

6878
var b7: Book & number = { foreword: "hi", price: 10.99 };
69-
~~~~~~~~~~~~
79+
~~
7080
!!! error TS2322: Type '{ foreword: string; price: number; }' is not assignable to type 'Book & number'.
71-
!!! error TS2322: Object literal may only specify known properties, and 'price' does not exist in type 'Book & number'.
81+
!!! error TS2322: Type '{ foreword: string; price: number; }' is not assignable to type 'number'.
7282

7383
var b8: Cover | Cover[] = { couleur : "non" };
7484
~~~~~~~~~~~~~~~
@@ -91,4 +101,37 @@ tests/cases/compiler/objectLiteralExcessProperties.ts(33,27): error TS2322: Type
91101
!!! error TS2322: Type '{ colour: string; }' is not assignable to type 'Cover'.
92102
!!! error TS2322: Object literal may only specify known properties, but 'colour' does not exist in type 'Cover'. Did you mean to write 'color'?
93103
!!! related TS6501 tests/cases/compiler/objectLiteralExcessProperties.ts:28:5: The expected type comes from this index signature.
104+
105+
// Repros inspired by #28752
106+
107+
function test<T extends IFoo>() {
108+
~~~~
109+
!!! error TS2304: Cannot find name 'IFoo'.
110+
// No excess property checks on generic types
111+
const obj1: T = { name: "test" };
112+
~~~~
113+
!!! error TS2322: Type '{ name: string; }' is not assignable to type 'T'.
114+
// No excess property checks on intersections involving generics
115+
const obj2: T & { prop: boolean } = { name: "test", prop: true };
116+
~~~~
117+
!!! error TS2322: Type '{ name: string; prop: boolean; }' is not assignable to type 'T & { prop: boolean; }'.
118+
!!! error TS2322: Type '{ name: string; prop: boolean; }' is not assignable to type 'T'.
119+
// Excess property checks only on non-generic parts of unions
120+
const obj3: T | { prop: boolean } = { name: "test", prop: true };
121+
~~~~~~~~~~~~
122+
!!! error TS2322: Type '{ name: string; prop: boolean; }' is not assignable to type 'T | { prop: boolean; }'.
123+
!!! error TS2322: Object literal may only specify known properties, and 'name' does not exist in type '{ prop: boolean; }'.
124+
// Excess property checks only on non-generic parts of unions
125+
const obj4: T & { prop: boolean } | { name: string } = { name: "test", prop: true };
126+
~~~~~~~~~~
127+
!!! error TS2322: Type '{ name: string; prop: boolean; }' is not assignable to type '{ name: string; } | (T & { prop: boolean; })'.
128+
!!! error TS2322: Object literal may only specify known properties, and 'prop' does not exist in type '{ name: string; }'.
129+
// No excess property checks when union includes 'object' type
130+
const obj5: object | { x: string } = { z: 'abc' }
131+
// The 'object' type has no effect on intersections
132+
const obj6: object & { x: string } = { z: 'abc' }
133+
~~~~~~~~
134+
!!! error TS2322: Type '{ z: string; }' is not assignable to type 'object & { x: string; }'.
135+
!!! error TS2322: Object literal may only specify known properties, and 'z' does not exist in type 'object & { x: string; }'.
136+
}
94137

tests/baselines/reference/objectLiteralExcessProperties.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,23 @@ interface Indexed {
3232
var b10: Indexed = { 0: { }, '1': { } }; // ok
3333

3434
var b11: Indexed = { 0: { colour: "blue" } }; // nested object literal still errors
35+
36+
// Repros inspired by #28752
37+
38+
function test<T extends IFoo>() {
39+
// No excess property checks on generic types
40+
const obj1: T = { name: "test" };
41+
// No excess property checks on intersections involving generics
42+
const obj2: T & { prop: boolean } = { name: "test", prop: true };
43+
// Excess property checks only on non-generic parts of unions
44+
const obj3: T | { prop: boolean } = { name: "test", prop: true };
45+
// Excess property checks only on non-generic parts of unions
46+
const obj4: T & { prop: boolean } | { name: string } = { name: "test", prop: true };
47+
// No excess property checks when union includes 'object' type
48+
const obj5: object | { x: string } = { z: 'abc' }
49+
// The 'object' type has no effect on intersections
50+
const obj6: object & { x: string } = { z: 'abc' }
51+
}
3552

3653

3754
//// [objectLiteralExcessProperties.js]
@@ -46,3 +63,18 @@ var b8 = { couleur: "non" };
4663
var b9 = { forewarned: "still no" };
4764
var b10 = { 0: {}, '1': {} }; // ok
4865
var b11 = { 0: { colour: "blue" } }; // nested object literal still errors
66+
// Repros inspired by #28752
67+
function test() {
68+
// No excess property checks on generic types
69+
var obj1 = { name: "test" };
70+
// No excess property checks on intersections involving generics
71+
var obj2 = { name: "test", prop: true };
72+
// Excess property checks only on non-generic parts of unions
73+
var obj3 = { name: "test", prop: true };
74+
// Excess property checks only on non-generic parts of unions
75+
var obj4 = { name: "test", prop: true };
76+
// No excess property checks when union includes 'object' type
77+
var obj5 = { z: 'abc' };
78+
// The 'object' type has no effect on intersections
79+
var obj6 = { z: 'abc' };
80+
}

0 commit comments

Comments
 (0)