Skip to content

Commit f4a5562

Browse files
committed
wip: possible fixes
1 parent 7e57c81 commit f4a5562

File tree

5 files changed

+151
-6
lines changed

5 files changed

+151
-6
lines changed

src/compiler/checker.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26061,12 +26061,17 @@ namespace ts {
2606126061
links.resolvedSignature = cached;
2606226062
return type;
2606326063
}
26064+
2606426065
const contextualSignature = getContextualSignature(func);
2606526066
if (contextualSignature) {
26067+
// const inferenceContext = getInferenceContext(func); // >> Changed here
26068+
// const signature = inferenceContext ?
26069+
// instantiateSignature(contextualSignature, inferenceContext.mapper) : contextualSignature;
26070+
const signature = contextualSignature;
2606626071
const index = func.parameters.indexOf(parameter) - (getThisParameter(func) ? 1 : 0);
2606726072
return parameter.dotDotDotToken && lastOrUndefined(func.parameters) === parameter ?
26068-
getRestTypeAtPosition(contextualSignature, index) :
26069-
tryGetTypeAtPosition(contextualSignature, index);
26073+
getRestTypeAtPosition(signature, index) :
26074+
tryGetTypeAtPosition(signature, index);
2607026075
}
2607126076
}
2607226077

@@ -31858,21 +31863,23 @@ namespace ts {
3185831863
if (links.type === unknownType) {
3185931864
links.type = getTypeFromBindingPattern(declaration.name);
3186031865
}
31861-
assignBindingElementTypes(declaration.name);
31866+
assignBindingElementTypes(declaration.name, links.type);
3186231867
}
3186331868
}
3186431869
}
3186531870

3186631871
// When contextual typing assigns a type to a parameter that contains a binding pattern, we also need to push
3186731872
// the destructured type into the contained binding elements.
31868-
function assignBindingElementTypes(pattern: BindingPattern) {
31873+
function assignBindingElementTypes(pattern: BindingPattern, parentType: Type) {
3186931874
for (const element of pattern.elements) {
3187031875
if (!isOmittedExpression(element)) {
31876+
const type = getBindingElementTypeFromParentType(element, parentType); // >> Changed here
3187131877
if (element.name.kind === SyntaxKind.Identifier) {
31872-
getSymbolLinks(getSymbolOfNode(element)).type = getTypeForBindingElement(element);
31878+
// getSymbolLinks(getSymbolOfNode(element)).type = getTypeForBindingElement(element);
31879+
getSymbolLinks(getSymbolOfNode(element)).type = type;
3187331880
}
3187431881
else {
31875-
assignBindingElementTypes(element.name);
31882+
assignBindingElementTypes(element.name, type);
3187631883
}
3187731884
}
3187831885
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
// ~~~ Property 'bar' does not exist on type 'Omit<T, "foo">'. ts(2339)
15+
});
16+
17+
//// [narrowingRestGenericCall.js]
18+
var __rest = (this && this.__rest) || function (s, e) {
19+
var t = {};
20+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
21+
t[p] = s[p];
22+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
23+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
24+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
25+
t[p[i]] = s[p[i]];
26+
}
27+
return t;
28+
};
29+
function call(obj, cb) {
30+
cb(obj);
31+
}
32+
call(obj, function (_a) {
33+
var foo = _a.foo, rest = __rest(_a, ["foo"]);
34+
console.log(rest.bar);
35+
// ~~~ Property 'bar' does not exist on type 'Omit<T, "foo">'. ts(2339)
36+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
// ~~~ Property 'bar' does not exist on type 'Omit<T, "foo">'. ts(2339)
45+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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); // ~~~ Property 'bar' does not exist on type 'Omit<T, "foo">'. ts(2339)}) : void
27+
>call : <T extends object>(obj: T, cb: (val: T) => void) => void
28+
>obj : Slugs
29+
>({foo, ...rest}) => { console.log(rest.bar); // ~~~ Property 'bar' does not exist on type 'Omit<T, "foo">'. ts(2339)} : ({ 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+
// ~~~ Property 'bar' does not exist on type 'Omit<T, "foo">'. ts(2339)
43+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
// ~~~ Property 'bar' does not exist on type 'Omit<T, "foo">'. ts(2339)
14+
});

0 commit comments

Comments
 (0)