microsoft / microsoft/TypeScript

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

Open
#40,979 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: JSDoc
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the JavaScript example in the linked TypeScript Playground, comparing the @type {null} field with the working string | null form. Trace how JSDoc @type annotations are handled for constructor-assigned class fields; done means the null field is inferred as null rather than producing an implicit-any error.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.