microsoft / dts-gen

dts-gen creates starter TypeScript definition files for any module or library.
MIT License
2.44k stars 102 forks source link

How to generate .d.ts for ES6 Modules ? #42

Open ashubham opened 7 years ago

ashubham commented 7 years ago
(function (exports, require, module, __filename, __dirname) { 
  import {NativeModules, DeviceEventEmitter, Platform} from 'react-native';
                                                              ^^^^^^
SyntaxError: Unexpected token import

I have a third party library written in ES6, how could I generate typings for this ?

highruned commented 7 years ago

Any resolution on this?

nisimjoseph commented 7 years ago

when es6 suppose will be available? thank you

geminiyellow commented 7 years ago
dts-gen --expression-file "./node_modules/react-native-animatable/index.js"
Unexpected crash! Please log a bug with the commandline you specified.
/usr/local/lib/node_modules/dts-gen/bin/lib/run.js:130
        throw e;
        ^

SyntaxError: Unexpected token import
Dimous commented 7 years ago

dts-gen is pretty much useless @msftgits

julianguyen commented 6 years ago

Is this going to be supported?

vvs commented 6 years ago

Doesn't seem to be working on ES6 modules at all. :( This would be very useful for initial transition from js to ts.

Zazck commented 6 years ago

add below to dts-gen-master/lib/run.ts

// @ts-ignore
require("babel-polyfill");
// @ts-ignore
require('babel-register')({
    cache: false,
});

and npm run build link project to npm now dts your code by dts-gen -m <your-project-name> 🎉

for expressions

import { transform, TransformOptions } from 'babel-core';
const babelOpt: TransformOptions = {
    extends: `${process.cwd()}/.babelrc`,
}
/** ... **/
} else if (args.expression) {
    name = args.name || 'dts_gen_expr';
    result = guess.generateIdentifierDeclarationFile(name, eval(transform(args.expression, babelOpt).code || ''));
} else if (args['expression-file']) {
    if (args.name) throw new ArgsError('Cannot use -name with -expression-file');
    const filename = args['expression-file'];
    name = path.basename(filename, path.extname(filename)).replace(/[^A-Za-z0-9]/g, '_');
    (module as any).paths.unshift(process.cwd() + '/node_modules');
    const fileContent = fs.readFileSync(filename, "utf-8");
    result = guess.generateIdentifierDeclarationFile(name, eval(transform(fileContent, babelOpt).code || ''));

at least it works for me 🎉