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

Does ts-node/register work for destructuring? #43

Closed
eggers opened this issue Nov 30, 2015 · 11 comments
Closed

Does ts-node/register work for destructuring? #43

eggers opened this issue Nov 30, 2015 · 11 comments
Labels
external question Support requests. We tend not to answer these on Github. Nowadays I convert to Discussion instead.

Comments

@eggers
Copy link

eggers commented Nov 30, 2015

I'm getting this error right now when I use destructuring in my typescript code:

SyntaxError: Unexpected token [

Otherwise, it has been great.

@blakeembrey
Copy link
Member

What TypeScript version are you using it with? Can you post a reproducible test case?

That's a JavaScript error, so I think you're using this with a JavaScript file instead of a TypeScript file. Can you confirm that? Otherwise, I'd say it's the target output in tsconfig.json that's the issue (it's not supported with your node version).

@blakeembrey blakeembrey added the question Support requests. We tend not to answer these on Github. Nowadays I convert to Discussion instead. label Nov 30, 2015
@eggers
Copy link
Author

eggers commented Nov 30, 2015

@blakeembrey This is a problem with my mocha tests. As per some instructions I found somewhere, I added require('ts-node/register') at the top of my gulpfile, and all of my typescript code/tests works nicely with a simple .pipe(mocha());. (Including ones with language features like async/away and yield.) However, if I use destructuring in my typescript code, then the tests will fail with the error above.

And, yes I'm using node 4.2.1 which doesn't support destructuring by default yet.

@blakeembrey
Copy link
Member

Are the tests written in TypeScript or JavaScript. What about your tsconfig.json file - I guess you're targeting ES6? Is ts-node executing? I believe async/await and yield are all standard in node v4.

I'm not sure there's a solution if node doesn't support destructuring but does support things like async since TypeScript does not support as fine-grained tuning. If that's the case though, this is something to bring up with the TypeScript team (who I've seen discuss fine grained tuning in the past, so this just be an extra use-case on that).

@blakeembrey
Copy link
Member

Following up quickly, check out https://kangax.github.io/compat-table/es6/#test-destructuring. Looks like this is an issue, TypeScript will only compile "all" ES6 for us but node can't handle destructuring (maybe other things).

@eggers
Copy link
Author

eggers commented Nov 30, 2015

My tests are written in typescript. As far as I can tell, ts-node/register must be doing some kind of es5 transpilation because I'm pretty sure that node 4.2.1 doesn't support es6 imports yet (I also don't think that it supports async/await yet see: https://nodejs.org/en/docs/es6/), and I'm using those in my code & in my tests.

By the way, thanks for your work on the typings module, it's been a better solution that tsd for me so far.

@blakeembrey
Copy link
Member

My tests are written in typescript

You're right, I had to investigate and found that node currently doesn't support destructuring. I wish there wasn't something we could do here since that's a trivial issue for the compiler to handle, but TypeScript doesn't support feature flags. An existing discussion can be found at microsoft/TypeScript#4692, but it looks like it might have been derailed - I'll check in and see what's happening since this is a pretty helpful use-case.

By the way, thanks for your work on the typings module

Thanks so much! I'm glad it works for you, I haven't had a lot of feedback yet so it's hard to tell if I'm confusing people or if it's communicating the message correctly. There's a need for more corrected typings, so if you get a chance 😄

@eggers
Copy link
Author

eggers commented Nov 30, 2015

Yeah, I want to add some more typings because I'm having to install a lot of definitelytyped .d.ts files as ambient dependencies when I really want to save them as dev dependencies (a dev-ambient dependency might also help for things like mocha). I just want to get a better handle on it before I start making any pull requests. If you have, or were to make, a simple wiki page on how to create a typing definition project, I'd definitely start converting them.

I remember having a discussion when definitelytyped first came out that it would be better to have had each typing as a separate repo, but that met with a lot of resistance, so I'm really glad that you went in that direction...especially with the 1,385 folders that definitelytyped now handles.

@blakeembrey
Copy link
Member

a dev-ambient dependency might also help for things like mocha

Haha, yeah, you're probably correct. I originally had that parity, but removed it from typings before release. I can simply add it back, ambient dependencies aren't included with any installation anyway - they are just for environment "reference" here.

If you have, or were to make, a simple wiki page on how to create a typing definition project

I'll look at adding one for you (or to the FAQ in the README), but it's mostly straightforward - just moving everything inside the ambient declare module "x" to outside and creating a repo for it. There's a lot in https://github.com/typings, though I wrote most of them from scratch. If you ever hit an issue, you can always open a discussion.

You can always create the files/repos under your own user too, then just add them to the registry 😄 Supporting DefinitelyTyped as ambient is a nice use-case too, which I've avoided documenting but perhaps I should if people are discovering it anyway. It would probably solve all the TSD woes.

@blakeembrey
Copy link
Member

I'm going to reopen this for visibility or until it's fixed upstream in TypeScript

@frankwallis
Copy link

FYI - If you are using mocha you can enable destructuring and default parameters using

mocha --harmony --harmony-destructuring --harmony_default_parameters

@blakeembrey
Copy link
Member

Closing as answered, as far as I can tell it won't ever be fixed in TypeScript. Thankfully, ES6 works with node 6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external question Support requests. We tend not to answer these on Github. Nowadays I convert to Discussion instead.
Projects
None yet
Development

No branches or pull requests

3 participants