Home > Mobile >  Union in parameters changes error behavior
Union in parameters changes error behavior

Time:07-11

I bet this question has been asked before, but sadly have no good search terms.

Changing parameters to a union apparently also enables strict counting of parameters. This can:

a) make dummy parameters required, for no reason at all:

// Works.
const f: (...args: [x: string, y: boolean]                         ) => void = (    ) => {};
const g: (...args: [x: string, y: boolean]                         ) => void = (n   ) => {};
const h: (...args: [x: string, y: boolean]                         ) => void = (n, m) => {};
// Still works.
const i: (...args: [x: string, y: boolean] | [x: string, y: symbol]) => void = (    ) => {};
const j: (...args: [x: string, y: boolean] | [x: string, y: symbol]) => void = (n, m) => {};
// Source has 2 element(s) but target allows only 1.(2322)
const k: (...args: [x: string, y: boolean] | [x: string, y: symbol]) => void = (n   ) => {};

b) allow imho invalid assignments, when there is no union:

// Works.
const f: (...args: any[]) => void = (...args: [number, string]                   ) => {};
// Target requires 2 element(s) but source may have fewer.(2322)
const g: (...args: any[]) => void = (...args: [number, string] | [string, number]) => {};

Why is this the case, and is there a way to enforce either behavior, what the parameters look like being irrelevant?


PS: with strictFunctionTypes, which is default these days

CodePudding user response:

It's a bug.

There is an open bug about it on GitHub.

  • Related