Skip to content

Commit b6ec37a

Browse files
TypeScript Botgabritto
TypeScript Bot
andauthored
Cherry-pick PR #47909 into release-4.6 (#47912)
Component commits: f4a5562 wip: possible fixes 75cb739 pass parameter type to assignBindingElementTypes 4a7de63 undo unnecessary changes b618cf9 update baselines Co-authored-by: Gabriela Araujo Britto <[email protected]>
1 parent 83efc9f commit b6ec37a

File tree

5 files changed

+138
-4
lines changed

5 files changed

+138
-4
lines changed

src/compiler/checker.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -31832,21 +31832,22 @@ namespace ts {
3183231832
if (links.type === unknownType) {
3183331833
links.type = getTypeFromBindingPattern(declaration.name);
3183431834
}
31835-
assignBindingElementTypes(declaration.name);
31835+
assignBindingElementTypes(declaration.name, links.type);
3183631836
}
3183731837
}
3183831838
}
3183931839

3184031840
// When contextual typing assigns a type to a parameter that contains a binding pattern, we also need to push
3184131841
// the destructured type into the contained binding elements.
31842-
function assignBindingElementTypes(pattern: BindingPattern) {
31842+
function assignBindingElementTypes(pattern: BindingPattern, parentType: Type) {
3184331843
for (const element of pattern.elements) {
3184431844
if (!isOmittedExpression(element)) {
31845+
const type = getBindingElementTypeFromParentType(element, parentType);
3184531846
if (element.name.kind === SyntaxKind.Identifier) {
31846-
getSymbolLinks(getSymbolOfNode(element)).type = getTypeForBindingElement(element);
31847+
getSymbolLinks(getSymbolOfNode(element)).type = type;
3184731848
}
3184831849
else {
31849-
assignBindingElementTypes(element.name);
31850+
assignBindingElementTypes(element.name, type);
3185031851
}
3185131852
}
3185231853
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//// [narrowingRestGenericCall.ts]
2+
interface Slugs {
3+
foo: string;
4+
bar: string;
5+
}
6+
7+
function call<T extends object>(obj: T, cb: (val: T) => void) {
8+
cb(obj);
9+
}
10+
11+
declare let obj: Slugs;
12+
call(obj, ({foo, ...rest}) => {
13+
console.log(rest.bar);
14+
});
15+
16+
//// [narrowingRestGenericCall.js]
17+
var __rest = (this && this.__rest) || function (s, e) {
18+
var t = {};
19+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
20+
t[p] = s[p];
21+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
22+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
23+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
24+
t[p[i]] = s[p[i]];
25+
}
26+
return t;
27+
};
28+
function call(obj, cb) {
29+
cb(obj);
30+
}
31+
call(obj, function (_a) {
32+
var foo = _a.foo, rest = __rest(_a, ["foo"]);
33+
console.log(rest.bar);
34+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
=== tests/cases/compiler/narrowingRestGenericCall.ts ===
2+
interface Slugs {
3+
>Slugs : Symbol(Slugs, Decl(narrowingRestGenericCall.ts, 0, 0))
4+
5+
foo: string;
6+
>foo : Symbol(Slugs.foo, Decl(narrowingRestGenericCall.ts, 0, 17))
7+
8+
bar: string;
9+
>bar : Symbol(Slugs.bar, Decl(narrowingRestGenericCall.ts, 1, 14))
10+
}
11+
12+
function call<T extends object>(obj: T, cb: (val: T) => void) {
13+
>call : Symbol(call, Decl(narrowingRestGenericCall.ts, 3, 1))
14+
>T : Symbol(T, Decl(narrowingRestGenericCall.ts, 5, 14))
15+
>obj : Symbol(obj, Decl(narrowingRestGenericCall.ts, 5, 32))
16+
>T : Symbol(T, Decl(narrowingRestGenericCall.ts, 5, 14))
17+
>cb : Symbol(cb, Decl(narrowingRestGenericCall.ts, 5, 39))
18+
>val : Symbol(val, Decl(narrowingRestGenericCall.ts, 5, 45))
19+
>T : Symbol(T, Decl(narrowingRestGenericCall.ts, 5, 14))
20+
21+
cb(obj);
22+
>cb : Symbol(cb, Decl(narrowingRestGenericCall.ts, 5, 39))
23+
>obj : Symbol(obj, Decl(narrowingRestGenericCall.ts, 5, 32))
24+
}
25+
26+
declare let obj: Slugs;
27+
>obj : Symbol(obj, Decl(narrowingRestGenericCall.ts, 9, 11))
28+
>Slugs : Symbol(Slugs, Decl(narrowingRestGenericCall.ts, 0, 0))
29+
30+
call(obj, ({foo, ...rest}) => {
31+
>call : Symbol(call, Decl(narrowingRestGenericCall.ts, 3, 1))
32+
>obj : Symbol(obj, Decl(narrowingRestGenericCall.ts, 9, 11))
33+
>foo : Symbol(foo, Decl(narrowingRestGenericCall.ts, 10, 12))
34+
>rest : Symbol(rest, Decl(narrowingRestGenericCall.ts, 10, 16))
35+
36+
console.log(rest.bar);
37+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
38+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
39+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
40+
>rest.bar : Symbol(Slugs.bar, Decl(narrowingRestGenericCall.ts, 1, 14))
41+
>rest : Symbol(rest, Decl(narrowingRestGenericCall.ts, 10, 16))
42+
>bar : Symbol(Slugs.bar, Decl(narrowingRestGenericCall.ts, 1, 14))
43+
44+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
=== tests/cases/compiler/narrowingRestGenericCall.ts ===
2+
interface Slugs {
3+
foo: string;
4+
>foo : string
5+
6+
bar: string;
7+
>bar : string
8+
}
9+
10+
function call<T extends object>(obj: T, cb: (val: T) => void) {
11+
>call : <T extends object>(obj: T, cb: (val: T) => void) => void
12+
>obj : T
13+
>cb : (val: T) => void
14+
>val : T
15+
16+
cb(obj);
17+
>cb(obj) : void
18+
>cb : (val: T) => void
19+
>obj : T
20+
}
21+
22+
declare let obj: Slugs;
23+
>obj : Slugs
24+
25+
call(obj, ({foo, ...rest}) => {
26+
>call(obj, ({foo, ...rest}) => { console.log(rest.bar);}) : void
27+
>call : <T extends object>(obj: T, cb: (val: T) => void) => void
28+
>obj : Slugs
29+
>({foo, ...rest}) => { console.log(rest.bar);} : ({ foo, ...rest }: Slugs) => void
30+
>foo : string
31+
>rest : { bar: string; }
32+
33+
console.log(rest.bar);
34+
>console.log(rest.bar) : void
35+
>console.log : (...data: any[]) => void
36+
>console : Console
37+
>log : (...data: any[]) => void
38+
>rest.bar : string
39+
>rest : { bar: string; }
40+
>bar : string
41+
42+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
interface Slugs {
2+
foo: string;
3+
bar: string;
4+
}
5+
6+
function call<T extends object>(obj: T, cb: (val: T) => void) {
7+
cb(obj);
8+
}
9+
10+
declare let obj: Slugs;
11+
call(obj, ({foo, ...rest}) => {
12+
console.log(rest.bar);
13+
});

0 commit comments

Comments
 (0)