-
-
Notifications
You must be signed in to change notification settings - Fork 543
handle case where '*.d.ts' files are compiled and there is no output #207
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
Conversation
fixes sporadic cases of 'TypeError: Cannot read property 'text' of undefined' we were seeing when writing declaration files for existing CoffeeScript code.
2 similar comments
|
Sorry, I've rejected these PRs in the past because it is wrong. |
|
Does the message in https://github.com/TypeStrong/ts-node/pull/208/files make sense? |
|
Wow! I was in the middle of responding and suggesting something similar 😄 It does not entirely make sense. For one, I'm |
|
I also wanted to add that my code works with In my case, we have a Thanks for the rapid response and assistance! Let me know if I can do anything to help. I do think that adding an error message is a big improvement. But it doesn't explain why we only see this problem when running our code through ts-node and mocha. |
|
So the important thing here is that TypeScript will load the definition anyway, that doesn't cause any errors, but this code only ever runs when the So how does this happen? It happens when you |
|
Sadly, I've been unable to repro this outside of our considerably large and complex repository. And it manifests itself at odd times. It's definitely something I'd like to understand better. And maybe we need to change how we add type declarations to our legacy CoffeeScript code. In our case, we do have runtime values because we've previously required If this isn't enough info to go on, I understand. I'll see if I can reproduce the issue in a clean repo. |
|
Do you know what file it's failing on also? Because there shouldn't be any case where |
|
Hey @blakeembrey. I'm @ryanwe's colleague. Great work on ts-node! And thanks for the good discussion. I agree this is pretty odd, and I see what you're saying. But I also wanted to chime in and clarify that this...
...isn't what we're doing. I'll explain our setup and hopefully that'll help. In your https://github.com/pillarjs/path-to-regexp project, your /// <reference path="typings/index.d.ts" />
...
import pathToRegexp = require('./index')(Just FYI that the Our code looks instead like this: import someModule from './someModule'
(I know The key difference between our code and
http://palantir.github.io/tslint/rules/no-reference/ With the rationale to use ES6-style And then the TypeScript manual says that for any ES6-style https://www.typescriptlang.org/docs/handbook/module-resolution.html#how-typescript-resolves-modules I'm not 100% sure if this answers the open questions for why we've been encountering this, etc., but I hope this at least sheds some light. Let us know if we can answer any more questions! And thanks again for your help. |
|
@aseemk The The reference here is irrelevant, but that is the correct usage unless you add a I also understand how TypeScript resolves The only thing I can do to help is hopefully get the filenames you're running into this. E.g. this is the entry, this is the require it dies on. A minimal repro would be good, because a lot of things you've pointed out aren't really relevant though the situation makes sense. |
|
Just to clarify further, in case I haven't emphasised this, that code never runs from TypeScript. It is only run when node.js encounters a |
|
Shoot, actually, I think I see what's going on. Give me a sec to write it out, but the PR doesn't fix it while understanding the error from https://github.com/TypeStrong/ts-node/pull/208/files, I think, would. |
|
Actually, ignore me, the case I was thinking of was if you had Having the entry file and the require would absolutely help me. Once I release a version with that error, it might help you. |
|
Thanks for all the great explanations, @blakeembrey. They make sense. Good news: @ryanwe may have found the cause! He's still confirming, I think, but it's likely that we are inadvertantly https://github.com/aseemk/requireDir To support e.g. CoffeeScript and other things, this module looks at Now that we know that's not the case for (edit: our) I'm curious: how often do |
|
They can never declare anything, |
|
The way I always explain it is that a |
|
Great, that's helpful, thanks. @ryanwe is patching So hopefully we're set after this. Thank you again! |
|
So, basically, yeah, it sucks that error existed in that case you thought should be good, but it's usually an indicator you have a bigger bug on your hands. In this case, it really wouldn't have mattered either way since you'd only be requiring for side-effects if you're loading a directory like that. I'm not sure how valuable it would be to say, yes, let's just make it pass, because if it's happening it's pretty likely you're doing something wrong (just not in this case). If anyone feels strongly about it though, definitely let me know 😄 |
|
Thanks again @blakeembrey! Glad we got this straightened up. In light of |
|
I think we should reconsider this because now |
|
No, it's not. Those two uses are completely different and you would never import a |
|
Oh, sorry that I misunderstood the rule. I now wonder what is really a right way to import a |
|
Hmm, depends. I also recommend using it with |
|
Thanks. I'm writing a library to be published into npm. The directory structure is like below: Source files, declare namespace X {
export type A = { ... };
export type B = { ... };
export type C = A | B;
...
}In sources, the types are used like When I specify the What's the right way to include |
|
I resolved this with making |
|
@noraesae I'd also recommend, if those types are intended for your own package, that you write it in module format instead of global. Otherwise you'd be polluting the global namespace for everyone using your project and that may create collisions. For example, I sometimes just create an |
This fixes sporadic cases of
TypeError: Cannot read property 'text' of undefinedwe were seeing when writing declaration files for existing CoffeeScript code. We're running TypeScript 2.0.3 with ts-node 1.3.0.We have a fairly log repo with a combination of CoffeeScript and TypeScript. Things would work fine when compiling with
tscand usingVSCode. But when running mocha tests withts:ts-node/registeras a compiler, we would occasionally see this type error.I was able to track the problem down to
getEmitOutput()returning empty output for type declaration files we added for interacting with our legacy CoffeeScript files.This fix will allow us again to
importthese type declarations instead ofrequireing the CoffeeScript modules. I tried to repro this issue as a new test in this repository but was unable to. If you have any suggestions on how to do so or would like me to make any additional changes, let me know.Thanks!