-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Fix check for overwritten properties in object spreads #44696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
tests/cases/conformance/types/spread/spreadDuplicate.ts(10,12): error TS2783: 'a' is specified more than once, so this usage will be overwritten. | ||
tests/cases/conformance/types/spread/spreadDuplicate.ts(12,12): error TS2783: 'a' is specified more than once, so this usage will be overwritten. | ||
|
||
|
||
==== tests/cases/conformance/types/spread/spreadDuplicate.ts (2 errors) ==== | ||
// Repro from #44438 | ||
|
||
declare let a: { a: string }; | ||
declare let b: { a?: string }; | ||
declare let c: { a: string | undefined }; | ||
declare let d: { a?: string | undefined }; | ||
|
||
declare let t: boolean; | ||
|
||
let a1 = { a: 123, ...a }; // string (Error) | ||
~~~~~~ | ||
!!! error TS2783: 'a' is specified more than once, so this usage will be overwritten. | ||
!!! related TS2785 tests/cases/conformance/types/spread/spreadDuplicate.ts:10:20: This spread always overwrites this property. | ||
let b1 = { a: 123, ...b }; // string | number | ||
let c1 = { a: 123, ...c }; // string | undefined (Error) | ||
~~~~~~ | ||
!!! error TS2783: 'a' is specified more than once, so this usage will be overwritten. | ||
!!! related TS2785 tests/cases/conformance/types/spread/spreadDuplicate.ts:12:20: This spread always overwrites this property. | ||
let d1 = { a: 123, ...d }; // string | number | ||
|
||
let a2 = { a: 123, ...(t ? a : {}) }; // string | number | ||
let b2 = { a: 123, ...(t ? b : {}) }; // string | number | ||
let c2 = { a: 123, ...(t ? c : {}) }; // string | number | ||
let d2 = { a: 123, ...(t ? d : {}) }; // string | number | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//// [spreadDuplicate.ts] | ||
// Repro from #44438 | ||
|
||
declare let a: { a: string }; | ||
declare let b: { a?: string }; | ||
declare let c: { a: string | undefined }; | ||
declare let d: { a?: string | undefined }; | ||
|
||
declare let t: boolean; | ||
|
||
let a1 = { a: 123, ...a }; // string (Error) | ||
let b1 = { a: 123, ...b }; // string | number | ||
let c1 = { a: 123, ...c }; // string | undefined (Error) | ||
let d1 = { a: 123, ...d }; // string | number | ||
|
||
let a2 = { a: 123, ...(t ? a : {}) }; // string | number | ||
let b2 = { a: 123, ...(t ? b : {}) }; // string | number | ||
let c2 = { a: 123, ...(t ? c : {}) }; // string | number | ||
let d2 = { a: 123, ...(t ? d : {}) }; // string | number | ||
|
||
|
||
//// [spreadDuplicate.js] | ||
"use strict"; | ||
// Repro from #44438 | ||
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 a1 = __assign({ a: 123 }, a); // string (Error) | ||
var b1 = __assign({ a: 123 }, b); // string | number | ||
var c1 = __assign({ a: 123 }, c); // string | undefined (Error) | ||
var d1 = __assign({ a: 123 }, d); // string | number | ||
var a2 = __assign({ a: 123 }, (t ? a : {})); // string | number | ||
var b2 = __assign({ a: 123 }, (t ? b : {})); // string | number | ||
var c2 = __assign({ a: 123 }, (t ? c : {})); // string | number | ||
var d2 = __assign({ a: 123 }, (t ? d : {})); // string | number | ||
|
||
|
||
//// [spreadDuplicate.d.ts] | ||
declare let a: { | ||
a: string; | ||
}; | ||
declare let b: { | ||
a?: string; | ||
}; | ||
declare let c: { | ||
a: string | undefined; | ||
}; | ||
declare let d: { | ||
a?: string | undefined; | ||
}; | ||
declare let t: boolean; | ||
declare let a1: { | ||
a: string; | ||
}; | ||
declare let b1: { | ||
a: string | number; | ||
}; | ||
declare let c1: { | ||
a: string | undefined; | ||
}; | ||
declare let d1: { | ||
a: string | number; | ||
}; | ||
declare let a2: { | ||
a: string | number; | ||
}; | ||
declare let b2: { | ||
a: string | number; | ||
}; | ||
declare let c2: { | ||
a: string | number; | ||
}; | ||
declare let d2: { | ||
a: string | number; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
=== tests/cases/conformance/types/spread/spreadDuplicate.ts === | ||
// Repro from #44438 | ||
|
||
declare let a: { a: string }; | ||
>a : Symbol(a, Decl(spreadDuplicate.ts, 2, 11)) | ||
>a : Symbol(a, Decl(spreadDuplicate.ts, 2, 16)) | ||
|
||
declare let b: { a?: string }; | ||
>b : Symbol(b, Decl(spreadDuplicate.ts, 3, 11)) | ||
>a : Symbol(a, Decl(spreadDuplicate.ts, 3, 16)) | ||
|
||
declare let c: { a: string | undefined }; | ||
>c : Symbol(c, Decl(spreadDuplicate.ts, 4, 11)) | ||
>a : Symbol(a, Decl(spreadDuplicate.ts, 4, 16)) | ||
|
||
declare let d: { a?: string | undefined }; | ||
>d : Symbol(d, Decl(spreadDuplicate.ts, 5, 11)) | ||
>a : Symbol(a, Decl(spreadDuplicate.ts, 5, 16)) | ||
|
||
declare let t: boolean; | ||
>t : Symbol(t, Decl(spreadDuplicate.ts, 7, 11)) | ||
|
||
let a1 = { a: 123, ...a }; // string (Error) | ||
>a1 : Symbol(a1, Decl(spreadDuplicate.ts, 9, 3)) | ||
>a : Symbol(a, Decl(spreadDuplicate.ts, 9, 10)) | ||
>a : Symbol(a, Decl(spreadDuplicate.ts, 2, 11)) | ||
|
||
let b1 = { a: 123, ...b }; // string | number | ||
>b1 : Symbol(b1, Decl(spreadDuplicate.ts, 10, 3)) | ||
>a : Symbol(a, Decl(spreadDuplicate.ts, 10, 10)) | ||
>b : Symbol(b, Decl(spreadDuplicate.ts, 3, 11)) | ||
|
||
let c1 = { a: 123, ...c }; // string | undefined (Error) | ||
>c1 : Symbol(c1, Decl(spreadDuplicate.ts, 11, 3)) | ||
>a : Symbol(a, Decl(spreadDuplicate.ts, 11, 10)) | ||
>c : Symbol(c, Decl(spreadDuplicate.ts, 4, 11)) | ||
|
||
let d1 = { a: 123, ...d }; // string | number | ||
>d1 : Symbol(d1, Decl(spreadDuplicate.ts, 12, 3)) | ||
>a : Symbol(a, Decl(spreadDuplicate.ts, 12, 10)) | ||
>d : Symbol(d, Decl(spreadDuplicate.ts, 5, 11)) | ||
|
||
let a2 = { a: 123, ...(t ? a : {}) }; // string | number | ||
>a2 : Symbol(a2, Decl(spreadDuplicate.ts, 14, 3)) | ||
>a : Symbol(a, Decl(spreadDuplicate.ts, 14, 10)) | ||
>t : Symbol(t, Decl(spreadDuplicate.ts, 7, 11)) | ||
>a : Symbol(a, Decl(spreadDuplicate.ts, 2, 11)) | ||
|
||
let b2 = { a: 123, ...(t ? b : {}) }; // string | number | ||
>b2 : Symbol(b2, Decl(spreadDuplicate.ts, 15, 3)) | ||
>a : Symbol(a, Decl(spreadDuplicate.ts, 15, 10)) | ||
>t : Symbol(t, Decl(spreadDuplicate.ts, 7, 11)) | ||
>b : Symbol(b, Decl(spreadDuplicate.ts, 3, 11)) | ||
|
||
let c2 = { a: 123, ...(t ? c : {}) }; // string | number | ||
>c2 : Symbol(c2, Decl(spreadDuplicate.ts, 16, 3)) | ||
>a : Symbol(a, Decl(spreadDuplicate.ts, 16, 10)) | ||
>t : Symbol(t, Decl(spreadDuplicate.ts, 7, 11)) | ||
>c : Symbol(c, Decl(spreadDuplicate.ts, 4, 11)) | ||
|
||
let d2 = { a: 123, ...(t ? d : {}) }; // string | number | ||
>d2 : Symbol(d2, Decl(spreadDuplicate.ts, 17, 3)) | ||
>a : Symbol(a, Decl(spreadDuplicate.ts, 17, 10)) | ||
>t : Symbol(t, Decl(spreadDuplicate.ts, 7, 11)) | ||
>d : Symbol(d, Decl(spreadDuplicate.ts, 5, 11)) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
=== tests/cases/conformance/types/spread/spreadDuplicate.ts === | ||
// Repro from #44438 | ||
|
||
declare let a: { a: string }; | ||
>a : { a: string; } | ||
>a : string | ||
|
||
declare let b: { a?: string }; | ||
>b : { a?: string | undefined; } | ||
>a : string | undefined | ||
|
||
declare let c: { a: string | undefined }; | ||
>c : { a: string | undefined; } | ||
>a : string | undefined | ||
|
||
declare let d: { a?: string | undefined }; | ||
>d : { a?: string | undefined; } | ||
>a : string | undefined | ||
|
||
declare let t: boolean; | ||
>t : boolean | ||
|
||
let a1 = { a: 123, ...a }; // string (Error) | ||
>a1 : { a: string; } | ||
>{ a: 123, ...a } : { a: string; } | ||
>a : number | ||
>123 : 123 | ||
>a : { a: string; } | ||
|
||
let b1 = { a: 123, ...b }; // string | number | ||
>b1 : { a: string | number; } | ||
>{ a: 123, ...b } : { a: string | number; } | ||
>a : number | ||
>123 : 123 | ||
>b : { a?: string | undefined; } | ||
|
||
let c1 = { a: 123, ...c }; // string | undefined (Error) | ||
>c1 : { a: string | undefined; } | ||
>{ a: 123, ...c } : { a: string | undefined; } | ||
>a : number | ||
>123 : 123 | ||
>c : { a: string | undefined; } | ||
|
||
let d1 = { a: 123, ...d }; // string | number | ||
>d1 : { a: string | number; } | ||
>{ a: 123, ...d } : { a: string | number; } | ||
>a : number | ||
>123 : 123 | ||
>d : { a?: string | undefined; } | ||
|
||
let a2 = { a: 123, ...(t ? a : {}) }; // string | number | ||
>a2 : { a: string | number; } | ||
>{ a: 123, ...(t ? a : {}) } : { a: string | number; } | ||
>a : number | ||
>123 : 123 | ||
>(t ? a : {}) : { a: string; } | {} | ||
>t ? a : {} : { a: string; } | {} | ||
>t : boolean | ||
>a : { a: string; } | ||
>{} : {} | ||
|
||
let b2 = { a: 123, ...(t ? b : {}) }; // string | number | ||
>b2 : { a: string | number; } | ||
>{ a: 123, ...(t ? b : {}) } : { a: string | number; } | ||
>a : number | ||
>123 : 123 | ||
>(t ? b : {}) : { a?: string | undefined; } | ||
>t ? b : {} : { a?: string | undefined; } | ||
>t : boolean | ||
>b : { a?: string | undefined; } | ||
>{} : {} | ||
|
||
let c2 = { a: 123, ...(t ? c : {}) }; // string | number | ||
>c2 : { a: string | number; } | ||
>{ a: 123, ...(t ? c : {}) } : { a: string | number; } | ||
>a : number | ||
>123 : 123 | ||
>(t ? c : {}) : { a: string | undefined; } | {} | ||
>t ? c : {} : { a: string | undefined; } | {} | ||
>t : boolean | ||
>c : { a: string | undefined; } | ||
>{} : {} | ||
|
||
let d2 = { a: 123, ...(t ? d : {}) }; // string | number | ||
>d2 : { a: string | number; } | ||
>{ a: 123, ...(t ? d : {}) } : { a: string | number; } | ||
>a : number | ||
>123 : 123 | ||
>(t ? d : {}) : { a?: string | undefined; } | ||
>t ? d : {} : { a?: string | undefined; } | ||
>t : boolean | ||
>d : { a?: string | undefined; } | ||
>{} : {} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sandersn I'm not sure why we were checking nullable and any/unknown flags here. Seems to me any non-optional property that duplicates a property to the left should trigger the error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there was a corner case where a type that included
undefined
was actually for an optional property, but wasn't marked as such. For the others, it might be thatunknown
and (sort of)any
includeundefined
.We can ship this in the beta and see if people complain. It's likely that with better optionality tracking, this is not a problem.