Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no-misused-generics: missing error for function value #690

Open
OliverJAsh opened this issue Mar 9, 2020 · 2 comments
Open

no-misused-generics: missing error for function value #690

OliverJAsh opened this issue Mar 9, 2020 · 2 comments

Comments

@OliverJAsh
Copy link

I would expect an error here. Or is the lint rule not designed to catch this?

// error ✅
type Fn = <T>(param: T) => number;

// no error ❌
const fn = <T>(param: T) => 1;
@ajafff
Copy link
Member

ajafff commented Mar 9, 2020

This is kinda working as intended. You could use the type parameter within the function body to ensure type safety or infer the return type:

function a<T>(param: T) {
  return param; // type parameter used in inferred return type
}

function b<T extends string>(param: Map<string, T>) {
  const v = param.get('foo')!;
  param.set('bar', v); // ok
  param.set('bar', 'v'); // should be an error, replacing the type parameter with its constraint makes this pass type checking
}

If you have a good idea how to handle such cases, especially the second case, I'm happy to change the rule.

@OliverJAsh
Copy link
Author

Good point. It would probably be easy to check that the generic is not used as the return type, but it could be difficult to detect if the generic is used in the function body in some other way, such as in your Map example. 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants