Skip to content

static return might fail with switch statement #23670

@ORESoftware

Description

@ORESoftware

I have this:

const onUpdate = function (v: string, o: any) : Promise<any> {
  
  const finalName = String(v || '').split('.')[1]; // collection name
  const _id = o && o.o2 && o.o2._id;
  let key;
  
  switch (finalName) {
    
    case 'categories':      
      return Category.findOne({_id}).exec().then(function (cat: any) {
         // ....
      });
    
    case 'acquisitions':
        return Acquisition.findOne({_id}).exec().then(function (acq: any) {
         // ...
      });
    
    case 'functionalTeams':
      return FunctionalGroup.findOne({_id}).exec().then(function (ft: any) {
           // ...
      });
    
    default:
      log.error('document collection did not match a pre-defined name');
      // nothing is returned here, but can TS check to see if it does/doesn't
  }
  
};

can TypeScript check to see if a Promise fails to be returned in the default or after the switch statement? Right now it's compiling even though a Promise is not returned in all cases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions