Skip to content

In JS object, 'this' assignment in object literal doesn't use contextual type #25926

Closed
@miluoshi

Description

@miluoshi

TypeScript Version: 2.9.2 (also tried typescript@next 3.1.0-dev.20180725)
VSCode version: 1.25.1

Search Terms: this argument jsdoc vscode

I am trying to add TypeScript typechecking using VSCode's intellisense + JSDoc annotation in my AngularJS project. I should be able to explicitly define a type of this keyword in a function by providing explicit this parameter in as described here.

Code

component.d.ts

import 'angular'

declare interface IComponentCtrl {
  $onChanges?(onChangesObj: ng.IOnChangesObject): void;
}
declare interface IComponent extends ng.IComponentOptions {
  controller(this: IComponentCtrl, ...args: any[]): void;
}

component.js

// @ts-check
/** @type {import('./component').IComponent} */
export const component = {
  controller() {
    /** @type {import('./component').IComponentCtrl} */
    const vm = this

    vm.$onChanges = (onChangesObj) => {} // onChangesObj: angular.IOnChangesObject

    this.$onChanges = (onChangesObj) => {} // onChangesObj: any
  }
}

Expected behavior:
When I define controller's type of this explicitly as interface with $onChanges method with argument of specific type, I should get type hints for the argument in the defined method.

Actual behavior:
The argument in this.$onChanges method has type any and the type checking works only if I assign this to another variable and explicitly add type annotation to the newly created variable.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: JSDocRelates to JSDoc parsing and type generationDomain: JavaScriptThe issue relates to JavaScript specificallycheckJsRelates to checking JavaScript using TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions