-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Rule proposal: disallow unused return types #6442
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
Comments
Haha, I was wondering if this would come up soon. Explicit vs. inferred return types has been the rage on TypeScript Twitter for a week or so now. Thanks for filing this issue - I'd been meaning to try to find or file one and hadn't gotten around to it yet. 😄 https://twitter.com/JoshuaKGoldberg/status/1622971899549720576:
|
Now that v8 is released, per #7936, we can mark this issue as unblocked! 🚀 |
This rule sounds interesting! Some questions/thoughts regarding this: Should the rule handle async function test(): Promise<string | null> {
return 'hello';
} If it should, should the following also be flagged? I think so: function test(): Promise<string | null> {
return Promise.resolve('hello');
} Should arrays (potentially others) also be reported? function test(): Array<string | null> {
return ['hello', 'world'];
} On a different topic, I think the rule may create some unwanted noise in some cases. Consider the following hypothetical example: // used throughout the project
export type Plugin = { kind: 'one' } | { kind: 'two' };
function createSomePlugin(): Plugin {
return {
kind: 'one',
};
} The function above violates the rule; however, it uses a referenced union type rather than inlining the union type on its return type annotation. Fixing the example above is not as trivial as fixing the simpler cases. In my opinion, cases like the one above shouldn't be flagged. I think the rule should only report a union that's declared as part of the function return type (inline), but not those on referenced types: // should report 🚫
function test(): string | null {
return 'hello'
}
// shouldn't report ✅
type R = string | null;
function test(): R {
return 'hello'
}
// should report 🚫
type R = string;
type T = number;
function test(): R | T {
return 'hello'
} Does this make sense? I'll be happy to hear opinions on this. |
My thoughts
Absolutely! What we've done before, though, is said specifically
Yep!
I think so! Special treatment makes sense here if possible.
Yeah, I generally agree that taking this heuristic-based approach makes sense. I think it makes sense in my head as well to inspect explicitly provided unions, and descend only into special generics, like The one thing that maybe we could consider is unpacking nullability.... For example, the following type NullableString = string | null | undefined;
function foo(): NullableString {
return "foo";
} could be detected and fixed to the following? function foo(): NonNullable<NullableString> {
return "foo";
} Another note is that I think we probably want to consider literals as satisfying their infinite type (which is sort of a corollary of the previous point) // I think this is ok, even though the return type _could_ just be `"foo" | "bar"`.
function foo() : string {
return Math.random() > 0.5 ? 'foo' : 'bar';
} But, // this is not ok
function foo(): "foo" | "bar" {
return "foo";
} |
Another syntax we could consider inspecting (if possible) is the fields within literal object types function foo(): { a: string | number } {
return { a: 'no numbers here' };
} |
Before You File a Proposal Please Confirm You Have Done The Following...
My proposal is suitable for this project
Description
Paired with explicit-function-return-types, it's possible to include extraneous return types that are never used. This can happen when refactoring code meaning that some code branches might become impossible to reach.
For example you might have this function:
where the return types are correct, but then it's refactored to
and let's assume it's being used in a codebase before param is required:
since the return type is unchanged, the null check branch might not be removed during refactoring, and neither the linter or tsc will complain about it.
Fail Cases
Pass Cases
Additional Info
It's totally possible I missed an issue that matched this proposal, but I couldn't find any.
The text was updated successfully, but these errors were encountered: