typescriptlinttslinttypescript-eslint

TS Lint rule to disallow multiple paramters of the same type


Is there any sort of linter that supports a rule that would disallow the following:

function testFn(a: string, b: string)

but would allow:

function testFn(a: number, b: string)

I've seen that eslint can set max params, but it's a max params of a certain type that I wish to disallow.

The reason is that it's easy to mix the params up and pass them in the same order. With typescript it will let me know if my params are backwards as the types don't match, but I wouldn't know with strings. In the case that I wish to pass two I would prefer to restructure them, so:

function testFn(params: { a: string, b: string })

Solution

  • There is no such rule right now. This rule request is a pretty specific feature request so it's not likely to be built into typescript-eslint core anytime soon. You can always create one yourself with the tooling at https://typescript-eslint.io/developers/custom-rules, then publish it as your own standalone ESLint plugin.

    By the way, just for visibility: "tslint" / "ts-lint" refers to the old deprecated TSLint project. TSLint was deprecated a few years back in favor of typescript-eslint, which is tooling that lets you run ESLint (and other common JS libraries) on TypeScript code.