Skip to content
This repository has been archived by the owner on Dec 15, 2023. It is now read-only.

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

Open
ashubham opened this issue Mar 10, 2017 · 7 comments
Open

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

ashubham opened this issue Mar 10, 2017 · 7 comments

Comments

@ashubham
Copy link

ashubham commented Mar 10, 2017

(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 ?

@aerykk
Copy link

aerykk commented Jun 14, 2017

Any resolution on this?

@nisimjoseph
Copy link

when es6 suppose will be available? thank you

@geminiyellow
Copy link

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
Copy link

Dimous commented Sep 23, 2017

dts-gen is pretty much useless
@msftgits

@julianguyen
Copy link

Is this going to be supported?

@vvs
Copy link

vvs commented Jun 21, 2018

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

@Zazck
Copy link

Zazck commented Aug 18, 2018

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 🎉

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants