Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Union in parameters changes error behavior

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

// 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

>Solution :

It’s a bug.

There is an open bug about it on GitHub.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading