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.
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.
"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
https://github.com/Microsoft/TypeScript/issues/27261 Maybe reading this issue will help you to understand the use-case a little better
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! ๐