Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistency with Rest Parameter in Function Type #58552

Open
nlipiarski opened this issue May 16, 2024 · 2 comments
Open

Inconsistency with Rest Parameter in Function Type #58552

nlipiarski opened this issue May 16, 2024 · 2 comments

Comments

@nlipiarski
Copy link

πŸ”Ž Search Terms

rest parameters, parameter list, function parameters

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about parameters and rest parameters

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.4.5&ssl=5&ssc=12&pln=1&pc=1#code/C4TwDgpgBAysBOBLAdgcwGIFdkGNiIHtkoBeKACgDpqBDeVAZwC4oGEVUBtAXQEpSAfFABuBRABMA3AChpOImygQAHjQC2YADYQAjCzhI0WXPiKkKdVHtbs0-EkPnIGBbZU0FU5SzvcQ0wAAWvDLSKupauuQhQA

πŸ’» Code

type StringFunction = (...args: string[]) => void;

const example1: StringFunction = (arg1: string) => console.log(arg1.length);

example1();

πŸ™ Actual behavior

This code does not display any type errors on any of the versions I tried, but will break at run time on the example1() call.

πŸ™‚ Expected behavior

Typescript should prevent the assignment of (arg1: string) => void to a variable of type (...args: string[]) => void since it is possible to have args be empty.

Additional information about the issue

I attempted to search for other issues related to this and came up empty handed. This is different from the "I'm allowed to use a shorter parameter list where a longer one is expected" issue which has drowned out a lot of my searching.

Thanks in advance for any help with this.

@Andarist
Copy link
Contributor

I found this in the existing tests:

This is a pretty old test though and it assumes strictNullChecks: false. I think it doesn't quite make sense in the strict world for this to be allowed. Rest params are effectively optional so why do we observe a difference here?

// error
const fn1: (a?: string) => void = (a: string) => {}

// ok?
const fn2: (...args: number[]) => void = (x: number) => {};

We can find some recent tests with strict: true that would break if the above wouldn't work:

function foo<T extends string[]>(
fa: (s: string, ...args: string[]) => string,
fb: (s: string, ...args: T) => string
) {
const f1: (...args: any) => string = fa;
const f2: (...args: any[]) => string = fa;
const f3: (...args: any) => string = fb;
const f4: (...args: any[]) => string = fb;
}

At the very least f2 and f4 should error here - assuming that the issue at hand would get fixed.

@fatcerberus
Copy link

fatcerberus commented May 17, 2024

rest param corresponds to infinite number of params

Yeah, no. It can be up to an infinite number of params, but a minimum of zero. i.e. the same reason fn1 is an error in your example.

I guess the reasoning here is the same as how an object with a string index signature is treated in some contexts as having all possible properties at once, which is only rectified by enabling noUncheckedIndexedAccess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants