Skip to content

tsc produces invalid typings for parameter destructuring #3912

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
uber5001 opened this issue Jul 17, 2015 · 5 comments
Closed

tsc produces invalid typings for parameter destructuring #3912

uber5001 opened this issue Jul 17, 2015 · 5 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@uber5001
Copy link

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.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 17, 2015
@RyanCavanaugh
Copy link
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

@uber5001
Copy link
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?

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript and removed Duplicate An existing issue was already created labels Jul 17, 2015
@RyanCavanaugh
Copy link
Member

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

@ahejlsberg
Copy link
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.

@mhegazy mhegazy self-assigned this Jul 21, 2015
@mhegazy mhegazy added this to the TypeScript 1.7 milestone Jul 21, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Jul 21, 2015

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;
}

@uber5001 uber5001 changed the title Can function signatures lack parameter names? tsc produces invalid typings for parameter destructuring Jul 21, 2015
mhegazy added a commit that referenced this issue Aug 10, 2015
Fix #3912: emit declaration for binding elements correctelly
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Aug 10, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants