The standard idiom for this is to have a dependent template, that is specialized to std::false_type, like this:
template<int T> struct dependent_false : std::false_type {};
and then you can do:
template<int i>
void f() {
if constexpr (i == 1)
g();
else if constexpr (i == 2)
h();
else
static_assert(dependent_false<i>::value, "i can only be 1 or 2");
}
The reason you can't just say
static_assert(false, "i can only be 1 or 2");
is a rule in the language that says a branch of an if constexpr can't be false for every possible instantiation of the enclosing template.
Adding a template that could be specialized for std::true_type gets around this limitation.