grunt-ts
grunt-ts copied to clipboard
Transform export directory with interfaces
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".
Interfaces are stripped from the compiled Javascript, which is the cause of the error you're seeing.
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.
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?
I should have some time to investigate after the new year, but I'll be busy until then.
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).
+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');