Tslint: Support an option like `"typedef": "never"`

Created on 25 Sep 2018  ยท  10Comments  ยท  Source: palantir/tslint

Rule Suggestion

Is your rule for a general problem or is it specific to your development style?

This suggestion is about an existing rule which is called typedef and is a general problem for everyone especially the ones who wants to keep their code consistent.

What does your suggested rule do?

I suggest to add an option like "when": "never" and "when": "required" to the typedef-rule.
This would either force the user to never define types or, which is ways more important, to only define types when typescript can't figure them out on its own which is when the noImplicitAny-rule of tsc reports errors.

This is pretty useful to keep the code consistent and to make it looking the same no matter where.

List several examples where your rule could be used

"when": "always"
This should act the same like "typedef": true

"when": "required"

private name: string;

/* This should report an error because TypeScript
 * already knows its type from `this.name`
 *                v    */
public Name(): string
{
    return this.name;
}
let name: string = "Hello World";
//           ^
// This should report an error, too because also here TypeScript implicitly knows what type it is

Additional Context

https://github.com/Microsoft/TypeScript/issues/27261 Maybe reading this issue will help you to understand the use-case a little better

Rule Enhancement ๐ŸŒน R.I.P. ๐ŸŒน

All 10 comments

only define types when typescript can't figure them out on its own

The never option would be counterproductive in many situations now that type unions are available.

let color: "red" | "blue" = "red";
let nextId: number | undefined = numbers[0]; // numbers: number[] or [number, number]

...but having an option that indicates not to add the types when it doesn't change the type sounds like a good idea.

Ich hat's a good point!
This is exactly what I'm looking for.

Do you think it is possible to implement such an option?

I do ๐Ÿ˜Š as was mentioned in #4188, the typedef rule isn't what it used to be. TypeScript's type inference system is now smart enough that manual type annotations tend to be much more annoying than helpful. Allowing the rule to recommend _not_ having them might need the type checker as an optionally typed rule, but seems reasonable.

oh uhm... is it, by the way, up to me to write a proposal or is a TSLint-team member going to take care of it?

@manuth up to you ๐Ÿ˜Š

I just browsed through some issues which don't have the "Needs Proposal" label in order to find out what a proposal is.

Is it PR containing changes in order to implement my suggestion?

Not yet - we would just need to see in this issue a clear proposal of how you'd like to tackle these changes. In this case, I think it's just unclear when the "never" option would or wouldn't apply. If you could clarify that, ideally with some examples of when it would give a complaint and some examples of when it wouldn't, that should be good enough.

Does that clear it up?

Alright!
I'll give it a go this weekend. Thanks for your explanation.

โ˜ ๏ธ TSLint's time has come! โ˜ ๏ธ

TSLint is no longer accepting most feature requests per #4534. See typescript-eslint.io for the new, shiny way to lint your TypeScript code with ESLint. โœจ

It was a pleasure open sourcing with you all!

๐Ÿค– Beep boop! ๐Ÿ‘‰ TSLint is deprecated ๐Ÿ‘ˆ _(#4534)_ and you should switch to typescript-eslint! ๐Ÿค–

๐Ÿ”’ This issue is being locked to prevent further unnecessary discussions. Thank you! ๐Ÿ‘‹

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  ยท  3Comments

allbto picture allbto  ยท  3Comments

Ne-Ne picture Ne-Ne  ยท  3Comments

rajinder-yadav picture rajinder-yadav  ยท  3Comments

ypresto picture ypresto  ยท  3Comments