10LOC

#typescript-5-4

TypeScriptadvanced

Use NoInfer to keep generic inference predictable

export const createMethodGuard = <M extends string>(allowed: M[], fallback: NoInfer<M>) => {
  const allowedSet = new Set<string>(allowed);
  return (method: string): M => (allowedSet.has(method) ? (method as M) : fallback);
};

Prevent TypeScript from widening generic arguments when you need stable inference.