question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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:

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
sandersncommented, Oct 13, 2020

I can’t speak for the serializer code, since it’s much newer than getWidenedTypeForAssignmentDeclaration. However, the latter treats a lone null the same whether it comes from a type annotation or an initialiser – it’s regarded as an insufficient signal of intent and the code reports the error that @a-tarasyuk reports.

The correct fix is to treat an explicit type annotation differently from a null initialiser. However, I can’t think of a good reason for declaring a property of type null. @Raynos can you explain what you were using it for? Until we have a good, commonly used reason, I don’t feel like improving these semantics.

Meanwhile, I’m not sure what to do about the serialiser – it prioritises the type annotation for historical reasons. Ideally it should use the same rules as getWidenedTypeForAssignmentDeclaration, and one easy but expensive way to make that happen is to always request the actual type instead of short-circuiting on type annotation.

1reaction
Raynoscommented, Oct 14, 2020

@Raynos can you explain what you were using it for?

This workflow is for converting an existing JavaScript class into a JSDoc --checkJS // @ts-check class.

  • I have a large existing class I want to introduce type safety to.
  • The constructor is the first method in the class block
  • A field is null in the constructor but is then updated to be some concrete object in some asynchronous method further down in the class
  • I want to add a type annotation to every this field in the constructor so I can get explicit type checking
  • Added @type {null} is valid for the constructor to type check and remove the red errors.
  • There should now be an error in some other method further down the class where assigning a concrete object to null is a type error
  • I can find that error and then update the type annotation in the constructor to be @type {null | MyThing}

I don’t need a @type {null} but its a useful temporary annotation to remove red errors linearly in my text editor / tsc CLI as I’m going through a untyped file.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSDoc Reference - TypeScript: Documentation
JSDoc Reference. The list below outlines which constructs are currently supported when using JSDoc annotations to provide type information in JavaScript files.
Read more >
Use JSDoc: @type
Indicates that the value is of the specified type, but cannot be null . Variable number of that type, This function accepts a...
Read more >
Joshua's Docs - JSDoc Cheatsheet and Type Safety Tricks
If you want to annotate the type of variables that are assigned via destructuring, it can be a little complicated with JSDoc.
Read more >
Where is the syntax for TypeScript comments documented?
But you don't need to use the type annotation extensions in JSDoc. ... Note: you should not use JSDoc, as explained on TSDoc...
Read more >
Type Checking JavaScript Files - TypeScript
Likewise, when types can't be inferred, they can be specified using JSDoc the ... annotate a declaration in the constructor with JSDoc to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found