microsoft/dtslint

no-unnecessary-generics misbehaves with this

ro0gr opened this issue · 1 comments

ro0gr commented

Hi all,

First of all, Thanks for the work on this project!

I'm currently working on ambient types which needs to have a methods chainability support, like:

const a = factory({
  method: chainableMethod(),
});

a.method()
  .method();

my reduced version of typings for that:

  function factory<T>(config: T): FactoryResult<T>;

  type FactoryResult<T> = UnpackedDefinition<T> & {
    [s: string]: unknown;
  };

  type UnpackedDefinition<T> = {
    [k in keyof T]:
      T[k] extends MethodWrapper<infer C> ? C
      : T[k]
  };

  interface MethodWrapper<T extends <S>(this: S, ...args: any[]) => S> {
    get(): T;
  }

  function chainableMethod(): MethodWrapper<UnnecessaryGenericThis>;

  type UnnecessaryGenericThis = <T>(this: T) => T

Everything seems working fine(no TS errors, autocomplete works), but I get a dtslint error:

no-unnecessary-generics Type parameter T is never used.

It complains about:

   type UnnecessaryGenericThis = <T>(this: T) => T

where I try to return the current this from a function(is there another way to do it?).

I can get why such error occurs for arbitary args, but not sure about this.

Is it an error in dtslint or am I doing it wrong? Please let me know if I can provide more info!


Tested against:

  • dtslint: 0.5.3 and 1.0.2
  • typescript: 3.3.3333 and 3.6
ro0gr commented

This is super strange, I've just tried to re-install everyting with yarn(I used npm before) and seems like it works 🤔 Sorry for the noise.