Skip to content

Commit

Permalink
Merge pull request #7567 from Microsoft/contextual-type-parameters
Browse files Browse the repository at this point in the history
show completion in destructured parameter if containing function was contextually typed
  • Loading branch information
vladima committed Mar 17, 2016
2 parents f0b3ff1 + 112e4b1 commit 279fec7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3504,7 +3504,18 @@ namespace ts {
// We don't want to complete using the type acquired by the shape
// of the binding pattern; we are only interested in types acquired
// through type declaration or inference.
if (rootDeclaration.initializer || rootDeclaration.type) {
// Also proceed if rootDeclaration is parameter and if its containing function expression\arrow function is contextually typed -
// type of parameter will flow in from the contextual type of the function
let canGetType = !!(rootDeclaration.initializer || rootDeclaration.type);
if (!canGetType && rootDeclaration.kind === SyntaxKind.Parameter) {
if (isExpression(rootDeclaration.parent)) {
canGetType = !!typeChecker.getContextualType(<Expression>rootDeclaration.parent);
}
else if (rootDeclaration.parent.kind === SyntaxKind.MethodDeclaration || rootDeclaration.parent.kind === SyntaxKind.SetAccessor) {
canGetType = isExpression(rootDeclaration.parent.parent) && !!typeChecker.getContextualType(<Expression>rootDeclaration.parent.parent);
}
}
if (canGetType) {
typeForObject = typeChecker.getTypeAtLocation(objectLikeContainer);
existingMembers = (<BindingPattern>objectLikeContainer).elements;
}
Expand Down
35 changes: 35 additions & 0 deletions tests/cases/fourslash/objectLiteralBindingInParameter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/// <reference path="fourslash.ts"/>

////interface I { x1: number; x2: string }
////function f(cb: (ev: I) => any) { }
////f(({/*1*/}) => 0);

////[<I>null].reduce(({/*2*/}, b) => b);

////interface Foo {
//// m(x: { x1: number, x2: number }): void;
//// prop: I;
////}
////let x: Foo = {
//// m({ /*3*/ }) {
//// },
//// get prop(): I { return undefined; },
//// set prop({ /*4*/ }) {
//// }
////};

goTo.marker("1");
verify.completionListContains("x1");
verify.completionListContains("x2");

goTo.marker("2");
verify.completionListContains("x1");
verify.completionListContains("x2");

goTo.marker("3");
verify.completionListContains("x1");
verify.completionListContains("x2");

goTo.marker("4");
verify.completionListContains("x1");
verify.completionListContains("x2");

0 comments on commit 279fec7

Please sign in to comment.