Skip to content

How to prefer type Object<>, but otherwise object #709

Closed
@jaydenseric

Description

@jaydenseric

TypeScript doesn't recognise object<>, but understands Object<>. By default, eslint-plugin-jsdoc prefers the object type over Object. My intent is to prefer object when the type is not using <>, but if it is, prefer Object<>.

  1. Is TypeScript / VS Code intellisense behavior incorrect, and we should push them for a speedy fix?
  2. Should the defaults take this TypeScript / VS Code intellisense behavior into account, and prefer Object<>?
  3. How can that manually be configured, today?

Expected behavior

By default, the types enforced with jsdoc/check-types should be compatible with TypeScript / VS Code intellisense.

Actual behavior

The object<> type is not.

ESLint Config

{
  plugins: ['jsdoc'],
  rules: {
    'jsdoc/check-types': 'error'
  }
}

ESLint sample

To see the problem with the default prefered types, this will be an error:

/** @typedef {Object<string, boolean>} foo */

Screen Shot 2021-03-30 at 12 43 55 pm

To try to allow Object<>:

{
  plugins: ['jsdoc'],
  settings: {
    jsdoc: {
      preferredTypes: {
        'object<>': 'Object',
      },
    },
  },
  rules: {
    'jsdoc/check-types': 'error'
  }
}

Unfortunately, that results a never ending circle of autofix causing another error:

Screen Shot 2021-03-30 at 12 40 42 pm

Screen Shot 2021-03-30 at 12 40 23 pm

Environment

  • Node version: v15.12.0
  • ESLint version v7.23.0
  • eslint-plugin-jsdoc version: v32.3.0

Activity

brettz9

brettz9 commented on Mar 30, 2021

@brettz9
Collaborator

TypeScript doesn't recognise object<>, but understands Object<>. By default, eslint-plugin-jsdoc prefers the object type over Object. My intent is to prefer object when the type is not using <>, but if it is, prefer Object<>.

1. Is TypeScript / VS Code intellisense behavior incorrect, and we should push them for a speedy fix?

No, it's ok. We just felt that requiring "object" to include Object.create(null) was a better match since the lower-case form didn't suggest it needed to descend from Object (similar to how using Boolean and Number suggests wrapped objects).

object is for any non-primitive type per https://stackoverflow.com/a/49465172/271577 , but it seems it is not a generic (though neither is Object, requiring Record for some reason--I'm not really deep into TS, but I'd think using the doc examples in the playground would show the jsdoc type on hover instead of "any".).

But I'd generally be inclined to presume something like that, esp. being out for a while, is working as intended.

2. Should the defaults take this TypeScript / VS Code intellisense behavior into account, and prefer `Object<>`?

Yes, that is a good idea, though it seems it should perhaps be Object.<> with a dot since it is shown that way at https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html#type (it mentions standard JSDoc but JSDoc doesn't really advertise support without the dot). We can look to the mode/parser if we get the underlying support.

3. How can that manually be configured, today?

Unfortunately, we can't do this so easily with jsdoctypeparser's current AST traverser. It does not pass along info on the children or their being children (only the parent), so we'd have to do our own traversal.

brettz9

brettz9 commented on Jan 21, 2022

@brettz9
Collaborator

It seems we already make an exception to allow Object when the mode is "typescript" (or the parser is @typescript/eslint-parser).

brettz9

brettz9 commented on Jan 21, 2022

@brettz9
Collaborator

@jaydenseric : Maybe you want to offer your opinion on #800 ?

brettz9

brettz9 commented on Feb 3, 2022

@brettz9
Collaborator

Closing in favor of carrying forward discussion on #800 . Hopefully to have a fix soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @brettz9@jaydenseric

        Issue actions

          How to prefer type `Object<>`, but otherwise `object` · Issue #709 · gajus/eslint-plugin-jsdoc