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

Exporting a keyword-named function in a module #1784

Closed
panuhorsmalahti opened this issue Jan 23, 2015 · 5 comments
Closed

Exporting a keyword-named function in a module #1784

panuhorsmalahti opened this issue Jan 23, 2015 · 5 comments
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@panuhorsmalahti
Copy link

The type definition file for the Q module has the following problem:

 //export function try(method: Function, ...args: any[]): Promise<any>; // <- This is broken currently - not sure how to fix.

The compiler doesn't accept the type definition because it thinks a try/catch block is about to begin.

q/Q.d.ts(204,21): error TS1006: Identifier expected; 'try' is a keyword.
q/Q.d.ts(204,24): error TS1005: '{' expected.
q/Q.d.ts(204,72): error TS1005: '=>' expected.

Is there any way to define this exported function?

@mikaturunen
Copy link

👍
I need this information also.

@danquirk
Copy link
Member

This isn't allowed at the moment because JavaScript doesn't allow that pattern either. Presumably Q has actually defined it as something like module.exports.try = function() {}. We could start allowing you to use keywords for export var names to support this.

In the meantime one workaround would be to use a merged class and module.

declare class Q {
    static try();
}

declare module Q { }

Q.try();

The downside here is that now the type system will think Q has a construct signature and a prototype property.

@danquirk danquirk added the Bug A bug in TypeScript label Jan 23, 2015
@mhegazy mhegazy added this to the TypeScript 2.0 milestone Jan 30, 2015
@mhegazy mhegazy added Suggestion An idea for TypeScript and removed Bug A bug in TypeScript labels Apr 22, 2015
@mhegazy mhegazy removed this from the TypeScript 2.0 milestone Apr 22, 2015
@mhegazy mhegazy added the Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. label Dec 9, 2015
@adamvoss
Copy link

adamvoss commented Nov 5, 2016

I also encountered this or a variation thereof.

I was trying to do a literal port of the following JavaScript,

exports.delete = function(args) { ... }

but, could not use the following TypeScript

export function delete(args) { ... }

In the end I found the JavaScript version still works in the TypeScript file and had to stick with that for this function.

@DanielRosenwasser
Copy link
Member

Related is #9846. If I recall, we decided that it was too complex because you can rewrite your module as follows:

// Exported values go here.
interface X {
    delete(args: any[]): void;
}

// Exported types go here.
declare namespace x {
    interface Foo {
         // ...
    }
}

declare var x: X;
export = x;

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Nov 6, 2016

@vossad01, @DanielRosenwasser I'm curious why not use this instead.

function del(...args) { }

export { del as delete }

turns out this errors on import, sigh...

@mhegazy mhegazy added Fixed A PR has been merged for this issue and removed Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Feb 14, 2017
@mhegazy mhegazy added this to the TypeScript 2.2.1 milestone Feb 14, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

7 participants