grunt-ts icon indicating copy to clipboard operation
grunt-ts copied to clipboard

Transform export directory with interfaces

Open sebas2day opened this issue 9 years ago • 6 comments

If I have a file with the following I want to export:

interface IUser {
    username: string;
}
export = IUser;

With the 'ts:export' transformation, this doesn't work:

///ts:export=Directory
import IUser_file = require('./Directory/IUser'); ///ts:export:generated
export var IUser = IUser_file ///ts:export:generated

I get a syntax error: "Could not find symbol IUser_file".

sebas2day avatar Dec 04 '14 14:12 sebas2day

Interfaces are stripped from the compiled Javascript, which is the cause of the error you're seeing.

nexussays avatar Dec 05 '14 00:12 nexussays

I understand, but couldn't this be solved by generating the following:

export import IUser = require('./Directory/IUser');

No need for assigning it to a variable.

sebas2day avatar Dec 08 '14 11:12 sebas2day

Actually, yes. Good point. I think the TypeScript team fixed that bug in 1.1, so we should be able to remove all those redundant vars. @basarat or @nycdotnet do either of you have time to check into this?

nexussays avatar Dec 09 '14 16:12 nexussays

I should have some time to investigate after the new year, but I'll be busy until then.

nycdotnet avatar Dec 09 '14 18:12 nycdotnet

I think the TypeScript team fixed that bug in 1.1, so we should be able to remove all those redundant vars.

The bug is fixed. The _file placeholder can go away. @sebas2day PR welcome for this section of code : https://github.com/TypeStrong/grunt-ts/blob/master/tasks/modules/transformers.ts#L189-L190


If I have a file with the following I want to export:

Just FYI using export for pure interfaces is a bad idea as you get a JS file that is empty. Better to use a simple d.ts that just contains interfaces. See https://github.com/TypeStrong/grunt-ts/blob/master/tasks/modules/interfaces.d.ts in this repo (this repo does use commonjs otherwise).

basarat avatar Dec 10 '14 02:12 basarat

+1

It seems this bug (the export import) actually creates incorrect definition files.

Code:

///ts:export=Application

After transform:

import Application_file = require('./model/Application'); ///ts:export:generated
export var Application = Application_file; ///ts:export:generated

Generated .d.ts file:

import Application_file = require('./model/Application');
export declare var Application: typeof Application_file;

The typeof essentially breaks the export. Compare this with the export import functionality which maintains:

export import Application = require('./model/Application');

mtraynham avatar Dec 30 '14 17:12 mtraynham