Skip to content

Commit bd7d0f2

Browse files
authored
Don’t allow an object literal with a spread as a fallback for destructuring a property not present in all constituents (#43783)
* Add test * Don’t allow an object literal with a spread as a fallback for destructuring a property not present in all constituents
1 parent c552a4b commit bd7d0f2

File tree

6 files changed

+72
-1
lines changed

6 files changed

+72
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11698,7 +11698,7 @@ namespace ts {
1169811698
checkFlags |= CheckFlags.WritePartial | (indexInfo.isReadonly ? CheckFlags.Readonly : 0);
1169911699
indexTypes = append(indexTypes, isTupleType(type) ? getRestTypeOfTupleType(type) || undefinedType : indexInfo.type);
1170011700
}
11701-
else if (isObjectLiteralType(type)) {
11701+
else if (isObjectLiteralType(type) && !(getObjectFlags(type) & ObjectFlags.ContainsSpread)) {
1170211702
checkFlags |= CheckFlags.WritePartial;
1170311703
indexTypes = append(indexTypes, undefinedType);
1170411704
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests/cases/compiler/destructuringFromUnionSpread.ts(5,9): error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
2+
3+
4+
==== tests/cases/compiler/destructuringFromUnionSpread.ts (1 errors) ====
5+
interface A { a: string }
6+
interface B { b: number }
7+
8+
declare const x: A | B;
9+
const { a } = { ...x } // error
10+
~
11+
!!! error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
12+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [destructuringFromUnionSpread.ts]
2+
interface A { a: string }
3+
interface B { b: number }
4+
5+
declare const x: A | B;
6+
const { a } = { ...x } // error
7+
8+
9+
//// [destructuringFromUnionSpread.js]
10+
var __assign = (this && this.__assign) || function () {
11+
__assign = Object.assign || function(t) {
12+
for (var s, i = 1, n = arguments.length; i < n; i++) {
13+
s = arguments[i];
14+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
15+
t[p] = s[p];
16+
}
17+
return t;
18+
};
19+
return __assign.apply(this, arguments);
20+
};
21+
var a = __assign({}, x).a; // error
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/destructuringFromUnionSpread.ts ===
2+
interface A { a: string }
3+
>A : Symbol(A, Decl(destructuringFromUnionSpread.ts, 0, 0))
4+
>a : Symbol(A.a, Decl(destructuringFromUnionSpread.ts, 0, 13))
5+
6+
interface B { b: number }
7+
>B : Symbol(B, Decl(destructuringFromUnionSpread.ts, 0, 25))
8+
>b : Symbol(B.b, Decl(destructuringFromUnionSpread.ts, 1, 13))
9+
10+
declare const x: A | B;
11+
>x : Symbol(x, Decl(destructuringFromUnionSpread.ts, 3, 13))
12+
>A : Symbol(A, Decl(destructuringFromUnionSpread.ts, 0, 0))
13+
>B : Symbol(B, Decl(destructuringFromUnionSpread.ts, 0, 25))
14+
15+
const { a } = { ...x } // error
16+
>a : Symbol(a, Decl(destructuringFromUnionSpread.ts, 4, 7))
17+
>x : Symbol(x, Decl(destructuringFromUnionSpread.ts, 3, 13))
18+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/destructuringFromUnionSpread.ts ===
2+
interface A { a: string }
3+
>a : string
4+
5+
interface B { b: number }
6+
>b : number
7+
8+
declare const x: A | B;
9+
>x : A | B
10+
11+
const { a } = { ...x } // error
12+
>a : any
13+
>{ ...x } : { a: any; } | { a: any; b: number; }
14+
>x : A | B
15+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
interface A { a: string }
2+
interface B { b: number }
3+
4+
declare const x: A | B;
5+
const { a } = { ...x } // error

0 commit comments

Comments
 (0)