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

Validate arg count minimum count cannot be implemented with patterns? #734

Open
EliLichtblau opened this issue Feb 20, 2023 · 2 comments
Open

Comments

@EliLichtblau
Copy link
Contributor

This code talked about in the issue is in: https://github.com/dudykr/stc/blob/main/crates/stc_ts_file_analyzer/src/analyzer/expr/call_new.rs

The current min_required_pat implementation has the clause

RPat::Rest(p) => {
                    if p.type_ann.is_some() {
                        return 0;
                    }

So the following code:

declare function foo(a: string, ...b: [number, number]);

is given 1 required parameter. This is an issue that can be solved within the pattern (I think), however code that looks like

type alias = [number, number]
declare function foo(a: string, ...b: T);

cannot be resolved because T is a type reference and I don't think pattern stores information to resolve this. I think this means we have to count required parameters with types. I don't know if this is a big performance hit or if I'm just misunderstanding the problem. I'm going to work on a type based implementation but if my understanding is completely wrong please let me know!

@EliLichtblau
Copy link
Contributor Author

On second thought I'm not sure types are the right way to do this either since Type::params don't seem to store their resolved types. I think the issue is arity checks need to happen as the types are resolved for type params. Like

declare function foo2<T extends unknown[]>(a: T, ...b: T);
const tupleObj: [string, number] = ["dog", 3] 
foo2(tupleObj)

When T is first resolved to [string, number] it should trigger an arity check. Rather than it keeping going and T eventually being resolved to []

@kdy1
Copy link
Member

kdy1 commented Feb 21, 2023

I think it can be handled with Type because we don't have resolved types while checking arity for first time, but we uses Type which is not Type::Param after we resolve the overload.

@kdy1 kdy1 added this to the v0.0.1: Correctness milestone Mar 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants