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

Bug: Typescript error in default value for new rule schema #7412

Open
4 tasks done
wespickett opened this issue Aug 4, 2023 · 3 comments
Open
4 tasks done

Bug: Typescript error in default value for new rule schema #7412

wespickett opened this issue Aug 4, 2023 · 3 comments
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: utils Issues related to the @typescript-eslint/utils package

Comments

@wespickett
Copy link
Contributor

wespickett commented Aug 4, 2023

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Issue Description

In a new rule meta.schema an entry of, for example:

{
                type: 'object',
                properties: {
                  allowedTypeAnnotations: {
                    type: 'array',
                    items: { type: 'string' },
                    default: [],
                  },
                },
                additionalProperties: false,
 },

Gives an error for the default value of [], where I expect it to be a valid value
This is due to missing types for JSONSchema4Array and JSONSchema4Object in JSONSchema4Type.
Those types exist in json-schema: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/json-schema/index.d.ts#L30

PR with the fix:
#7406

Reproduction Repository Link

https://github.com/wespickett/typescript-eslint-ex1/tree/main

Repro Steps

  1. clone the repo
  2. npm install
  3. Open in an editor like VSCode to see the error in example-rule.ts, or run npx tsc

Versions

package version
@typescript-eslint/eslint-plugin 6.3.0
@typescript-eslint/parser 6.3.0
@typescript-eslint/scope-manager 6.3.0
@typescript-eslint/typescript-estree 6.3.0
@typescript-eslint/type-utils 6.3.0
@typescript-eslint/utils 6.3.0
TypeScript 5.1.6
ESLint 8.46.0
node 18.14.2
@wespickett wespickett added bug Something isn't working triage Waiting for maintainers to take a look labels Aug 4, 2023
@wespickett
Copy link
Contributor Author

@bradzacher Issue created as per your comment #7406 (comment)

@bradzacher bradzacher added package: utils Issues related to the @typescript-eslint/utils package accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for maintainers to take a look labels Aug 9, 2023
@bradzacher
Copy link
Member

Thanks for filing this!
With the examples I can see your intended code and why it's broken!

It's a rarer usecase to use default - a lot of codebases don't use it because (a) they don't know about it and (b) because its interaction with types or complex schemas can be somewhat confusing!

It's also not possible to enforce that the default defined in the schema is correctly defined to match the expected types.

We generally recommend that rules instead use defaultOptions to explicitly declare the options to use as you can enforce that the value passed there adheres to the options type you define for the rule, eg:

defaultOptions: [
{
default: 'array',
},
],

That being said - this is indeed a bug!

@JoshuaKGoldberg
Copy link
Member

Coincidentally good timing: eslint/eslint#17448 discusses default options & schemas a bit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: utils Issues related to the @typescript-eslint/utils package
Projects
None yet
Development

No branches or pull requests

3 participants