Cannot annotate a field in constructor as null using jsdoc @type

未关闭
#40,979 7 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
领域
compilers

调研方向

在链接的 TypeScript Playground 中重现 JavaScript 示例,将 @type {null} 字段与可正常工作的 string | null 形式进行比较。跟踪 JSDoc @type 注解如何处理在构造函数中赋值的类字段;完成标准是 null 字段被推断为 null,而不是产生隐式 any 错误。

由索引模型根据 Issue 内容生成。

描述

Bug Domain: JSDoc

TypeScript Version: 4.0.2

Search Terms: jsdoc null type implicit any

Code

class Foo {
    constructor () {
        /** @type {number} */
        this.bar = 42

        /** @type {null} */
        this.foo = null
    }
}

Expected behavior:

Expected to have a class with two fields, bar of type number & foo of type null

I don't know why it doesn't understand null; using string | null works fine.

Actual behavior: Member 'foo' implicitly has an 'any' type.

Playground Link: https://www.typescriptlang.org/play?useJavaScript=true#code/MYGwhgzhAEBiD29oG8BQ0PWPAdhALgE4Cuw+8h0AFAJQrqaMD0AVC9AAL4CeADgKYocxALYAjfoQC+0FkwaMM+ABYBLCADoxYSgF5oAFgBMqBYtbsufQcmEgQMuWcYr1GgGaJo+uyDNTUKSA

Related Issues:

主要语言
Go
星标
111k
派生
14.4k
平均合并
1 天 19 小时
30 天内合并 PR
117

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

microsoft/TypeScript 的其他 Issue

查看 microsoft/TypeScript 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。