Microsoft/TypeScript

Inconsistent behaviour when accessing setters between TypeScript (.ts files) and TypeScript in JSDoc.

Open

#47,603 创建于 2022年1月25日

在 GitHub 查看
 (2 评论) (0 反应) (0 负责人)TypeScript (6,726 fork)batch import
BugDomain: JSDocHelp WantedcheckJs

仓库指标

Star
 (48,455 star)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 9 个 PR)

描述

Bug Report

🔎 Search Terms

Most major ones is these but haven't had much luck in finding anyone with the same issue.

  • Inconsistent behaviour between Typescript and TypeScript in JSDoc
  • Inconsistent behaviour when accessing setters between TypeScript and JSDoc

Could be good to note that I've made an StackOverflow question. So if you try searching yourself and you find that one, that one was from me.

🕗 Version & Regression Information

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

I tried the following versions:

  • v4.6.0-dev.20220125
  • v4.5.5
  • v4.5.4
  • v4.4.4

⏯ Playground Link

Since my problem lies in the TypeScript in JSDoc, it will have to be a repo. But I promise it is very simple.

💻 Code

Simple showcase of the problem, please note that this is a JavaScript file:

export class Input extends HTMLElement {
  static get observedAttributes() {
    return ['label']
  }

  /**
   * @param {string} text
   */
  set label(text) {
    const span = this.shadowRoot?.querySelector('span')
    if (span) span.textContent = text
  }

  /**
   * @param {'label'} name
   * @param {string | null} _oldValue
   * @param {string | null} newValue
   */
  attributeChangedCallback(name, _oldValue, newValue) {
    this[name] = newValue || '' // Duplicate property 'label' ts(2718)

    // This line works fine but note the `this.label` instead of `this[name]` which I prefer.
    // this.label = newValue || ''
  }
}

🙁 Actual behavior

Typescript can't seem to recognize that label is a setter when using a variable for specifying the property name. My guess is that it is thinks that I'm trying to define a property named label.

🙂 Expected behavior

I expect no Duplicare property 'label'. Reason for expecting this is that I tried converting my file to TypeScript and I received no errors.

They are very similar but the difference is that instead of specifying the parameter types in JSDoc, I did it what we usually do in TypeScript like this (you can find the whole TypeScript file in the repo):

attributeChangedCallback(name: 'label', _oldValue: string | null, newValue: string | null) {
  this[name] = newValue || '' // works fine
}

贡献者指南