Skip to content

tsc produces invalid typings for parameter destructuring #3912

Closed

Description

I have an interface that is supposed to have a function signature of:

(param: {prop: boolean}): any

but I forget the parameter name (I'm not sure if this signature is considered valid):

({prop: boolean}): any

When compiling this invalid interface with the --declaration flag, an invalid .d.ts file results:

/* foo.ts */
export interface SomeInterface {
    ({prop: boolean}): any
}

tsc foo.ts --module commonjs --declaration

/* foo.d.ts */
export interface SomeInterface {
    ({prop: }: {
        prop: any;
    }): any;
}

If parameter names are optional, this should produce a valid .d.ts file. If parameter names are required, tsc should throw an error, or at least warn me that there is an issue.

Activity

  1. RyanCavanaugh commented on Jul 17, 2015

    @RyanCavanaugh
    Member

    Parameter names are required in some form or another. {prop: boolean} is a destructuring parameter (a new ES6 feature) that takes the first argument and assigns its prop property into the boolean variable.

    See #2023 for the bug about destructured parameters creating invalid .d.ts files

  2. uber5001 commented on Jul 17, 2015

    @uber5001
    Author

    Hey Ryan. I don't think this is a duplicate of #2023, as I'm not attempting to destructure anything here. Since parameter names are required (thank you for clarifying!), the issue is now that tsc does not throw any errors, and silently produces invalid output.

    This was tested using the latest version of tsc, taken from master/bin/tsc. Some sort of error should be produced at compile-time instead of generating invalid output, right?

  3. added
    BugA bug in TypeScript
    and removed
    DuplicateAn existing issue was already created
    on Jul 17, 2015
  4. RyanCavanaugh commented on Jul 17, 2015

    @RyanCavanaugh
    Member

    You're right. Thanks for following up -- I misread the linked bug.

  5. ahejlsberg commented on Jul 21, 2015

    @ahejlsberg
    Member

    Sadly, this is not a bug. The ES6 destructuring syntax is eerily similar to the syntax for object types. The following is perfectly valid ES6:

    function foo({ prop: boolean }) {
        let x = boolean;  // Destructuring declares local named boolean of type any
    }
    
    foo({ prop: 42 });

    Not entirely clear how we'd make something like that an error.

  6. mhegazy commented on Jul 21, 2015

    @mhegazy
    Contributor

    Just to clarify, the issue here is that the declaration file generated is not valid. there is an extra ":" emitted after "prop" in:

    interface SomeInterface {
        ({prop: }: {
            prop: any;
        }): any;
    }
  7. changed the title [-]Can function signatures lack parameter names?[/-] [+]tsc produces invalid typings for parameter destructuring[/+] on Jul 21, 2015
  8. added a commit that references this issue on Jul 29, 2015
    ab42595
  9. added a commit that references this issue on Aug 10, 2015
    7f49375
  10. added a commit that references this issue on Aug 10, 2015
    9dc15b4
  11. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions