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

Lint: no-inferrable-types for a declared type #7549

Closed
rodolphocastro opened this issue Sep 18, 2020 · 3 comments
Closed

Lint: no-inferrable-types for a declared type #7549

rodolphocastro opened this issue Sep 18, 2020 · 3 comments

Comments

@rodolphocastro
Copy link

Hello!

Currently when running deno lint --unstable I get some issues with no-inferrable-types on fields that I'm actually declaring the type and then assigning a value.

For instance the following snippet causes the output from deno lint:

export function createString(
  prefix: string = "",
  params: GeneratorParameters = defaultParameters,
): string {
  if (!validateGeneratorParameters(params)) {
    throw new Error(`The parameters ${params} aren't valid`);
  }

  const suffix = generateRandomNumber(params).toString();
  return prefix !== "" ? prefix + suffix : suffix;
}
(no-inferrable-types) inferrable types are not allowed
  prefix: string = "",
  ^^^^^^^^^^^^^^^^^^^
    at E:\Src\dixture\src\_simpleGenerator.ts:45:2

Even tho the lint option is flagged as unstable I was unable to find this reported elsewhere so hopefully this is just a duplicate and this is a know issue 😅

You can find the failing snippet on this GitHub repository, just nevermind the no-explicit-any (as those are intended by me right now 😅)

@solson
Copy link
Contributor

solson commented Sep 18, 2020

This is actually working as intended according to eslint's definition of no-inferrable-types. Under "The following are invalid" they include this example:

function fn(a: number = 5, b: boolean = true) {}

So the idea of this lint is that even types in signatures should be removed when they are trivially inferrable from a literal value. There is an ignoreParameters option that makes it work how you expect, but I don't think deno lint allows configuration currently.

@solson
Copy link
Contributor

solson commented Sep 18, 2020

Also, in case it is unclear, note that the parameter does still end up with the expected type (hence why the lint considers declaring it redundant):

image

@rodolphocastro
Copy link
Author

rodolphocastro commented Sep 18, 2020

Thanks for the clarification @solson !

That makes sense haha. If only I hadn't included Deno as a hardcoded search term I might've ended up on eslint's docs 😅.

Update: lol, this is now on the first page of google for deno + inferrable-types so I guess there's something good about my poor googling skills leading to this issue hahaha 🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants