Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
Open
Description
Before You File a Proposal Please Confirm You Have Done The Following...
- I havesearched for related issues and found none that match my proposal.
- I have searched thecurrent rule list and found no rules that match my proposal.
- I haveread 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 withexplicit-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:
functionrandom(param?:string):string|null{if(typeofparam!=='string')returnnullreturn'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 unionfunctionrandom(param:string):string|null{return'has param'}
and let's assume it's being used in a codebase before param is required:
constrand=random(param)if(rand===null){thrownewError('...')}
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
functiontest():string|null{return'string'}classA{statictest():string|null{return'string'}test():string|null{return'string'}}
Pass Cases
functiontest():string{return'string'}classA{statictest():string|null{if(Math.random()<0.5)returnnullreturn'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.