Skip to content

Add function length into type system #20866

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

Closed
EliSnow opened this issue Dec 22, 2017 · 2 comments
Closed

Add function length into type system #20866

EliSnow opened this issue Dec 22, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@EliSnow
Copy link

EliSnow commented Dec 22, 2017

Currently the type system does not account for the length property in function types. In unions, where the length of the functions vary, it could be used as a discriminated union.

Here is an example that would work under this proposal:

type fn1 = (a: string, b: number) => void;
type fn2 = (a: number) => void;

function a (fn: fn1 | fn2) {
    if (fn.length == 1) {
       fn(7);
    } else {
        fn("foo", 8);
    }
}

Of course the type system would have to follow the same rules for determining a function's length as specified in the ECMAScript standard and described in this MDN description.

Without this functionality I am not aware of a way, besides using a very specific user-defined type guard, for the type system to distinguish between the function types.

@jwbay
Copy link
Contributor

jwbay commented Dec 22, 2017

Related: #18422

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Dec 22, 2017
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants