Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jaydenseric opened this issue Mar 30, 2021 · 4 comments
Closed

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

jaydenseric opened this issue Mar 30, 2021 · 4 comments

Comments

@jaydenseric
Copy link
Contributor

jaydenseric commented Mar 30, 2021

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
@brettz9
Copy link
Collaborator

brettz9 commented Mar 30, 2021

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
Copy link
Collaborator

brettz9 commented Jan 21, 2022

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

@brettz9
Copy link
Collaborator

brettz9 commented Jan 21, 2022

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

brettz9 added a commit to brettz9/eslint-plugin-jsdoc that referenced this issue Jan 21, 2022
brettz9 added a commit to brettz9/eslint-plugin-jsdoc that referenced this issue Jan 21, 2022
@brettz9
Copy link
Collaborator

brettz9 commented Feb 3, 2022

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

@brettz9 brettz9 closed this as completed Feb 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants