Open
Description
Before You File a Proposal Please Confirm You Have Done The Following...
- I have searched for related issues and found none that match my proposal.
- I have searched the current rule list and found no rules that match my proposal.
- I have read the FAQ and my problem is not listed.
My proposal is suitable for this project
- My proposal specifically checks TypeScript syntax, or it proposes a check that requires type information to be accurate.
- My proposal is not a "formatting rule"; meaning it does not just enforce how code is formatted (whitespace, brace placement, etc).
- I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).
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:
function random (param?: string): string | null {
if (typeof param !== 'string') return null
return 'has param'
}
where the return types are correct, but then it's refactored to
// note that param is no longer optional but the return type is the same union
function random (param: string): string | null {
return 'has param'
}
and let's assume it's being used in a codebase before param is required:
const rand = random(param)
if (rand === null) {
throw new Error('...')
}
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
function test (): string | null {
return 'string'
}
class A {
static test (): string | null {
return 'string'
}
test (): string | null {
return 'string'
}
}
Pass Cases
function test (): string {
return 'string'
}
class A {
static test (): string | null {
if (Math.random() < 0.5) return null
return 'string'
}
test (): string {
return 'string'
}
}
Additional Info
It's totally possible I missed an issue that matched this proposal, but I couldn't find any.