Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5d5da4c

Browse files
authored
Merge pull request microsoft#18925 from Microsoft/fix-spread-contextually-typed-by-binding-pattern
Fix object spread that is contextually typed by a binding pattern
2 parents b3d3b78 + 39fd0bf commit 5d5da4c

File tree

5 files changed

+92
-1
lines changed

5 files changed

+92
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14200,7 +14200,7 @@ namespace ts {
1420014200
// type with those properties for which the binding pattern specifies a default value.
1420114201
if (contextualTypeHasPattern) {
1420214202
for (const prop of getPropertiesOfType(contextualType)) {
14203-
if (!propertiesTable.get(prop.escapedName)) {
14203+
if (!propertiesTable.get(prop.escapedName) && !(spread && getPropertyOfType(spread, prop.escapedName))) {
1420414204
if (!(prop.flags & SymbolFlags.Optional)) {
1420514205
error(prop.valueDeclaration || (<TransientSymbol>prop).bindingElement,
1420614206
Diagnostics.Initializer_provides_no_value_for_this_binding_element_and_the_binding_element_has_no_default_value);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//// [spreadContextualTypedBindingPattern.ts]
2+
// #18308
3+
interface Person {
4+
naam: string,
5+
age: number
6+
}
7+
8+
declare const bob: Person
9+
declare const alice: Person
10+
11+
// [ts] Initializer provides no value for this binding element and the binding element has no default value.
12+
const { naam, age } = {...bob, ...alice}
13+
14+
15+
//// [spreadContextualTypedBindingPattern.js]
16+
var __assign = (this && this.__assign) || Object.assign || function(t) {
17+
for (var s, i = 1, n = arguments.length; i < n; i++) {
18+
s = arguments[i];
19+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
20+
t[p] = s[p];
21+
}
22+
return t;
23+
};
24+
// [ts] Initializer provides no value for this binding element and the binding element has no default value.
25+
var _a = __assign({}, bob, alice), naam = _a.naam, age = _a.age;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
=== tests/cases/conformance/types/spread/spreadContextualTypedBindingPattern.ts ===
2+
// #18308
3+
interface Person {
4+
>Person : Symbol(Person, Decl(spreadContextualTypedBindingPattern.ts, 0, 0))
5+
6+
naam: string,
7+
>naam : Symbol(Person.naam, Decl(spreadContextualTypedBindingPattern.ts, 1, 18))
8+
9+
age: number
10+
>age : Symbol(Person.age, Decl(spreadContextualTypedBindingPattern.ts, 2, 15))
11+
}
12+
13+
declare const bob: Person
14+
>bob : Symbol(bob, Decl(spreadContextualTypedBindingPattern.ts, 6, 13))
15+
>Person : Symbol(Person, Decl(spreadContextualTypedBindingPattern.ts, 0, 0))
16+
17+
declare const alice: Person
18+
>alice : Symbol(alice, Decl(spreadContextualTypedBindingPattern.ts, 7, 13))
19+
>Person : Symbol(Person, Decl(spreadContextualTypedBindingPattern.ts, 0, 0))
20+
21+
// [ts] Initializer provides no value for this binding element and the binding element has no default value.
22+
const { naam, age } = {...bob, ...alice}
23+
>naam : Symbol(naam, Decl(spreadContextualTypedBindingPattern.ts, 10, 7))
24+
>age : Symbol(age, Decl(spreadContextualTypedBindingPattern.ts, 10, 13))
25+
>bob : Symbol(bob, Decl(spreadContextualTypedBindingPattern.ts, 6, 13))
26+
>alice : Symbol(alice, Decl(spreadContextualTypedBindingPattern.ts, 7, 13))
27+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
=== tests/cases/conformance/types/spread/spreadContextualTypedBindingPattern.ts ===
2+
// #18308
3+
interface Person {
4+
>Person : Person
5+
6+
naam: string,
7+
>naam : string
8+
9+
age: number
10+
>age : number
11+
}
12+
13+
declare const bob: Person
14+
>bob : Person
15+
>Person : Person
16+
17+
declare const alice: Person
18+
>alice : Person
19+
>Person : Person
20+
21+
// [ts] Initializer provides no value for this binding element and the binding element has no default value.
22+
const { naam, age } = {...bob, ...alice}
23+
>naam : string
24+
>age : number
25+
>{...bob, ...alice} : { naam: string; age: number; }
26+
>bob : Person
27+
>alice : Person
28+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// #18308
2+
interface Person {
3+
naam: string,
4+
age: number
5+
}
6+
7+
declare const bob: Person
8+
declare const alice: Person
9+
10+
// [ts] Initializer provides no value for this binding element and the binding element has no default value.
11+
const { naam, age } = {...bob, ...alice}

0 commit comments

Comments
 (0)