Microsoft/TypeScript

Contextual 'this' parameter in JSDoc doesn't show correct completions after property assignment on `this` in function-valued object literal property

Open

#50,240 建立於 2022年8月9日

在 GitHub 查看
 (9 留言) (0 反應) (0 負責人)TypeScript (6,726 fork)batch import
BugDomain: JSDocHelp Wanted

倉庫指標

Star
 (48,455 star)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 9 個 PR)

描述

Bug Report

🔎 Search Terms

  • js this type
  • js this function property
  • function property this type

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "Functions"

⏯ Playground Link

N/A - involves JS code which cant be emulated in playground at the moment

💻 Code

Below are some examples of the issue including the tsconfig.json and the code. The code comments describe what suggestions the intellisense provide, and here is what is expected, ie the types showing up on this as defined in the ts file:

image

The issue is below, where if the function assigns any property to this then the intellisense for this is limited to those assigned properties and ignores the defined type, e.g.:

image

🙁 Actual behavior

If the function assigns any property to this then the intellisense for this is limited to those assigned properties and ignores the defined type.

🙂 Expected behavior

The issue above seems to only be for function properties with function declarations ie {property: function() {...}} if the function property uses shorthand ie {property() {...}} then the expected behaviour occurs regarding the typing of this, ie there is intellisense based on the defined type of this regardless of what happens in the function (from what I have tried).

For example: image

From my understanding the function declaration and shorthand notations shouldnt have different behaviour according to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions, so the typing also shouldnt behave differently.

Turning on checkJs fixes the issue however it means migrating to TS is more difficult as all the issues need to be fixed before getting any of the benefits e.g. intellisense, and this also limits functionality for large code bases migrating to TS but with a lot of legacy JS code.

貢獻者指南