Skip to content

Error thrown when parsing optional binding pattern parameters in function declaration #11725

@hjdivad

Description

@hjdivad

Bug Report

Current behavior

A function declaration with the declare keyword is disallowed from having an optional binding pattern parameter, unlike in TypeScript, where this is allowed.

Input Code

export declare function ohai(
    {
        foo
    }?: Args
): string;

Which is equivalent to what the typescript compiler will produce with --declaration for the following:

export function ohai(
    {
        foo = "FOOOO"
    }: Args = { }
): string {
    return `ohai ${foo}`;
};

Babel will throw the error "A binding pattern parameter cannot be optional in an implementation signature."

Expected behavior

Either Babel succeeds in parsing the code example above, or this case is added to the caveats section of the docs.

Environment

❯ npx envinfo --preset babel
npx: installed 1 in 1.437s

  System:
    OS: macOS 10.15.5
  Binaries:
    Node: 12.13.0 - ~/.volta/tools/image/node/12.13.0/bin/node
    Yarn: 1.17.3 - ~/.volta/tools/image/yarn/1.17.3/bin/yarn
    npm: 6.12.0 - ~/.volta/tools/image/node/12.13.0/bin/npm
  npmPackages:
    babel-eslint: ^10.0.3 => 10.1.0
    ember-cli-babel: ^7.17.2 => 7.21.0
    eslint: ^6.0.0 => 6.8.0


Possible Solution

I'm not particularly familiar with the parser code but I think broadly what needs to happen is for parseAssignableListItemTypes to not throw when the function declaration has the declare keyword, which would presumably be information passed in from parseAssignableListItem

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: typescriptoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issuepkg: parser

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions