The main point of the title is that I have come across this particular code:
type testNoArgsF = () => number;
type testArgsF = (arg1: boolean, arg2: string) => number;
type unknownArgsF = (...args: unknown[]) => number;
type anyArgsF = (...args: any[]) => number;
type testII = testArgsF extends anyArgsF ? true : false; // true
type testIII = Parameters<testArgsF> extends Parameters<unknownArgsF>
? true
: false; // true
// surprising:
type testIV = testArgsF extends unknownArgsF ? true : false; // false <- why?
// but:
type testV = testNoArgsF extends unknownArgsF ? true : false; // true
This piece of code is specifically in typescript (version 3.8), with strict mode enabled. What perplexes me is why a test function does not extend a function type with spread args of unknown[]
, even though their parameters do indeed extend unknown[]
. Given that the return type always remains as number, I am unsure about what other factor might be causing the extends
statement to turn out false.
Here are some additional observations:
- The extend statement holds true only if your test function has zero arguments.
- This unique behavior vanishes when you disable strict mode.