0

How do I make this compile.

type Matchmaker = (data: MatcherData, name: string) => string
type MatcherData = RegExp | Array<string>

function makeDataUser(fU: (data: MatcherData) => string): Matchmaker {
    return function (data: MatcherData, name: string): string {
      return fU(data) + name;
    }
}


const user1 = makeDataUser((data: MatcherData): string => {
    if (typeof data === 'regexp') {
        if ("lkajdsf".match(data)) {
            return "yes";
        }
        return "no";
    } else {
        return data.join('');
    }
});

const user2 = makeDataUser((data: MatcherData): string => {
    if (typeof data !== 'regexp') {
        return "yes";
    } else {
        return "no";
    }
}


const mRegexUser = user1(/regex/, 'name');
const mArrayUser = user2(['lkajf'], 'name2');

I want a function parameter to be either of two types and use it in either case.

The trick is I know user1 always expects RegExp, and user2 always expects Array<string>. But I want to provide them with a generic function above.

1
  • Is this what you're looking for? If not, please consider modifying the question to elaborate on what exactly the problem is.
    – jcalz
    Mar 7, 2021 at 5:05

1 Answer 1

0

If you switch the order of checking the type of data in the Matchmaker functions, the compiler will be able to narrow the type. You need to narrow it to either is array or is not array first, since as you found there is no typeof <regexp type>.

const user1 = makeDataUser((data: MatcherData): string => {
  if (Array.isArray(data)) {
    return data.join("");
  } else {
    if ("lkajdsf".match(data)) {
      return "yes";
    }
    return "no";
  }
});

const user2 = makeDataUser((data: MatcherData): string => {
  if (Array.isArray(data)) {
    return "no";
  } else {
    return "yes";
  }
});

If you later have more complex types, you could consider using a discriminated union, where you choose a key property (type below) to tell the compiler how to differentiate (discriminate) between the different branches of the union.

type Matchmaker = (data: MatcherData, name: string) => string;
type MatcherData =
  | {
      type: "RegExp";
      value: RegExp;
    }
  | {
      type: "Other";
      value: Array<string>;
    };

function makeDataUser(fU: (data: MatcherData) => string): Matchmaker {
  return function (data: MatcherData, name: string): string {
    return fU(data) + name;
  };
}

const user1 = makeDataUser((data: MatcherData): string => {
  if (data.type === "RegExp") {
    if ("lkajdsf".match(data.value)) {
      return "yes";
    }
    return "no";
  } else {
    return data.value.join("");
  }
});

const user2 = makeDataUser((data: MatcherData): string => {
  if (data.type === "RegExp") {
    return "yes";
  } else {
    return "no";
  }
});

const mRegexUser = user1({ type: "RegExp", value: /regex/ }, "name");
const mArrayUser = user2({ type: "Other", value: ["lkajf"] }, "name2");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.