-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Incorrect JS emitted for a simple case of a base/derived class in 1.5-beta #3414
Comments
This cannot be worked around, but will work if I turn the base class into an interface and thus am required to implement all its methods in all of derived classes. |
Also, this (not visible in the example project) is also a perfect example of the place where abstract classes could be useful, so once more 👍 for abstract classes (I know this should be coming in 2.0, but if it happens before, I won't be cross 😉 ) |
I maybe wrong, but as it stands at the moment, your |
@kitsonk The point of Unless I misunderstood the extent to which I find it very convenient not to have to |
But you don't have a |
@kitsonk Yes, of course. First, Second, if you put |
@ddotlic I think you must at least use |
@tinganho I do not, my 45 files prove that I don't 😄 Could someone from compiler team please comment on this? |
Possible duplicate of #21. |
@NoelAbrahams Thanks for pointing this out, I found a treasure trove of linked items from that one item. It appears that this is a known problem for which there is no fix at the moment. I see several items linked which partially aid in similar cases, but the issue still exists. I can confirm that manually adding Why would this still be a problem - compiler clearly sees all files, it's a question of emit order but this does not seem a too difficult thing to do (admittedly I'm oversimplifying)? Wasn't |
#2181 is a proposed solution for Visual Studio that I would like to see. It would do the following as suggested by @danquirk:
For non-VS compilation, I believe the solution is to list the items in the required order in |
I did not want to come to this conclusion as I've said many times in this thread (I like not having to list files in Can someone from compiler team please confirm this? Thanks @NoelAbrahams for additional info. |
The compiler doesn't do cross file binding automatically — imagine you having several classes named the same. How would the compiler know which you mean? |
Fair point. This is not necessarily TS issue as much as it is JS issue. It's also unexpected because the compiler appears to see the classes/files fine, just not order them in the emit phase. |
We can't determine the "right" order to run your code. For example, you could have some code that must run before class initialization, or some code that must run after class initialization. We have no idea what the affinity of any given piece of code to another is. /* File A */
let x = B;
class A {
static y = x;
}
/* File B */
class B extends A { } Clearly the correct order is to put File B before File A, because Now, you might say, that is a contrived example and the compiler should work in the "simple" cases. But this is worse than not trying to order at all, because if you depend on re-ordering for the "simple" cases, then enter the "complex" case (which is going to be a very subtle line of transition), then your code is going to break in a way that seems completely random. The proper fixes have been noted above and in linked threads: Use external modules, use reference comments on a per-file basis, have a single file with a list of reference comments that define the overall file ordering, or list the files in dependency order in tsconfig.json. |
While I don't personally like none of the proposed solutions, I can see why TypeScript cannot do much better than it already does. Some things in JavaScript are clearly unavoidable. Thanks everyone for an interesting discussion. |
I hope this is not a dupe, I was really surprised by this bug because - being so simple in nature - it appears it should have been caught by your tests (or at least stumbled upon by other people), hence there is a slight probability that I misunderstood something or that the compiler isn't capable to do the necessary work (at which point I'd be doubly disappointed).
Anyway, the example is artificial because it's extracted from a larger code base.
Imagine a simple project, let's start with
tsconfig.json
In the same folder, add a single file
c.ts
which is justclass C {}
Add a subfolder called
sub
and put the following two files inside:Then simply run tsc in the project folder (the one containing
tsconfig.json
andc.ts
)The output will be:
The problem is obvious - we're trying to use
B
before it is defined.If this has been noted and fixed, please forgive the noise.
If not, please try to look into fixing this before 1.5 is "done".
The text was updated successfully, but these errors were encountered: