1

I have a function type:

type Func = (param1: string, param2: number) => Promise<void>;

I want to validate functions using this type. But TS only throws error when the type is incompatible but not when the parameters are not present. Example:

const f1: Func = async () => {}; // need error here
const f2: Func = async (param1: string) => {}; // need error here
const f3: Func = async (param1: number) => {}; // TS throws error here only.
2
  • 1
    f1 and f2 are valid Funcs, both of them can be called fX("", 0) perfectly happily. Even if they declared both parameters, you can't force the implementation to use them, so what's the point? Commented Feb 27, 2024 at 10:08
  • There is no type Func which works the way you want. You could write some kind of generic helper function so that check(f) only works if f's arguments are known to be "exactly" [string ,number], but that's not how TS works. See the linked q/a for more info. Commented Feb 27, 2024 at 13:13

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.