Is there any workaround / solution in Eslint for TypeScript to get spaces in generic classes/methods

Viewed 404

I am trying to get Typescript ESLint to detect when there is no space between the angle brackets when you want to set a type to a generic method/class. The main goal is to get warnings / errors if the spaces are missing. Automatic fixing is just a nice to have.

For example this is the incorrect code:

class MyGenricClass<T> {
   MyGenericProperty: T;
   MyHttpResponse: <HttpResponse<T>>;
}

const InstancedClass = new MyGenricClass<string>();

I want the the generic types to have spaces inside the angle brackets like the following:

class MyGenricClass< T > {
   MyGenericProperty: T;
   MyHttpResponse: < HttpResponse< T > >;
}

const InstancedClass = new MyGenricClass< string >();

I've checked the following which I could find in TypeScript-Eslint documentation:

  • @typescript-eslint/func-call-spacing - just adds white space before the function call
  • @typescript-eslint/type-annotation-spacing - Works for let foo: string = "bar"; but not for the angle brackets
  • @typescript-eslint/space-before-function-paren - Feels the same as func-call-spacing

I've also tried to google and duckduckgo, without any success. Searched that I've done are:

  • typescript eslint space between generic
  • typescript eslint generic spacing
  • typescript eslint template spacing
  • typescript eslint generic type spacing
  • typescript eslint type annotation spacing

To be honest, I am not sure what's the correct naming for what I want to achieve. So I would appreciate some suggestions where I could look for a solution or a possible workaround. Worst case scenario is to write this rule myself, and create a PR to @typescript-eslint.

Many thanks in advance!

1 Answers

Oh, boy. This was rough.

First, I didn't know what they were called either. They're generics So, I did some googling myself. Ran into the same issue. The documentation of keyword-spacing in @typescript-eslint states that "This version adds support for generic type parameters on function calls.". But like, how?So I dig through the source code, because go figure. No dice.

Then, I check issues. That's where I found this.

Damnit.


So the search continues. Digging through some pulls comes up with func-call-spacing, claiming the same thing as the last one when it comes to generics.

Let's just say that it didn't pan out, either.


So, my conclusion is that it isn't going to be implemented in the main @typescript-eslint, but you could probably write an extension

Related