Home > OS >  how to remove 'any' keyword in generic constraint
how to remove 'any' keyword in generic constraint

Time:04-06

I have some problems to create a generic function without the any keyword which is not recommended by our linter. I tried to switch it against unknown, never because I don't really need to know the specific type at. But that just led to different errors.

Of course I could stay with any and just disable our linter for that, but I would really like to know if it could be done without it.

Here is the code, very simplified and modified for illustration purposes (TS Playground):

interface DialogContent<TResult> {
  close: Subject<TResult>;
}

function showDialog<T extends DialogContent<any /*what can be used here instead of any */>>(resolver: () => T)
{
  //...
}


class ExampleDialogContent implements DialogContent<void>{
  close = new Subject<void>();
}

showDialog<ExampleDialogContent>(() => new ExampleDialogContent());

CodePudding user response:

Using any is the right move in your case because no other type would work for creating a type that matches any form of the generic DialogContent type. Your best solution would be to suppress the lint via a comment.

  • Related