Function “lacks return statement” but has typeguards for all the paths

前端 未结 2 721
小蘑菇
小蘑菇 2021-01-18 05:22

I have the following interfaces and types (all of which are open to change)

interface Base {
    type: string;
}

interface A extends Base {
    type: \"A\";         


        
2条回答
  •  再見小時候
    2021-01-18 06:13

    If you're not going to throw any errors, and isC is your last rule check-point, couldn't you remove the condition (thus always returning the CThing at the end)?

    function doAThingBasedOnTheRuleType(rule: Rule): Thing {
        if (isAnd(rule)) {
            return DoAndThing(rule);
        }
        if (isOr(rule)) {
            return DoOrThing(rule);
        }
        if (isA(rule.base)) {
            return DoAThing(rule);
        }
        if (isB(rule.base)) {
            return DoBThing(rule);
        }
    
        return DoCThing(rule);
    }
    

提交回复
热议问题