-
Notifications
You must be signed in to change notification settings - Fork 262
Testing core-decorators.js using TypeScript #120
Comments
Oh, I see, it's the "main" in package.json that makes this work... Hmm. |
Since the tests run under node, they use the node resolution process (it also uses The The reason this is done is to make sure the tests are testing the same thing our users will use. I've seen cases where projects import directly and miss bugs that users hit, like something not being exported properly, etc. |
OK, I guess to make that work I'll have to put the TypeScript outputs into 'lib' instead of something typescript specific. I've run into some other interesting issues at compile time:
The first two are because there's TyepScript style typing in a JavaScript file. I'm assuming can comment that out -- short term. The later two are because TypeScript doesn't support decorators on class expressions. Is there some way to avoid that in the mixins decorator test? TypeScript now supports mixins natively, so perhaps that test isn't important. |
@BurtHarris hmm what's the end goal of this btw? to PR this so that the tests run under TypeScript I assume? There is some known issue with TypeScript and autobind #86 that I haven't had time to figure out. This would be neat to use to figure it out! re the override decorator having a type--that is an accident. I imagine I was just so used to giving type signatures in other projects that I typed it in my sleep. Can be removed since it's not actually checked anyway. re mixins, I think the test can easily be refactored to not need the decorator on a class expression at the expense of more duplication but not a huge deal. |
@jayphelps I'd like to see good TypeScript support in the package,* without requiring TypeScript to be used by calling code. As background, I'm considering using core-decorators to replace some stubbed-out decorators in my antlr4ts project, which is written in TypeScript. But I'm nervous as the core-decorates docs say it's not officially supported but doesn't get specific about the incompatibilities. So the more specific point my experiment is to get an accurate inventory of bugs impacting use of core-decorators from TypeScript, and perhaps fix them where it doesn't hurt the babel version. At this point think TypeScript is closer claiming close conformance with the stage 2 proposal. https://www.typescriptlang.org/docs/handbook/decorators.html * Note that I'm not saying the end goal is to port core-decorators to TypeScript files, that's your call however IMHO it wouldn't be a bad idea. It should be a simple matter to run tests of |
@BurtHarris all sounds good, except I want to make a critical distinction: I'm pretty confident TypeScript supports stage-0 decorators, just the same as core-decorators does. They have this quote:
But it's very misleading! It isn't saying TypeScript implements the stage-2 spec, it's just saying that "decorators" as a thing has reached stage-2. The spec changed very incompatibly between stage-0 and stage-2, and no compiler that I am aware of has implemented the stage-2 spec. The easiest way to know which spec they implement is to look at the arguments that the decorator is provided by the JS runtime. It varies depending on class vs property (and getter/setters) but let's just look at properties since it will make it obvious: In the case of stage-0 (and stage-1, I believe, but haven't confirmed) it is provided the target, property key as a string, then the property descriptor itself: function (target, key: string, descriptor: PropertyDescriptor);
interface PropertyDescriptor {
configurable: boolean,
enumerable: boolean,
value: any,
writable: boolean
} But in stage-2, it was changed to a single argument of a new, special "MemberDesciptor": function (descriptor: MemberDesciptor);
interface MemberDesciptor {
kind: "Property"
key: string,
isStatic: boolean,
descriptor: PropertyDescriptor,
extras?: MemberDescriptor[]
finisher?: (klass): void;
} There are other differences, but this is the easiest to quickly see which version you're working with. TypeScript implements the original, stage-0 spec and I haven't seen any public work to implement the stage-2 spec, probably because so many people depend on stage-0 at this point that they're going to wait for stage-3 (or 4) spec before breaking everyone. |
the latest issue with autobind and TypeScript is most likely related to the way they polyfill classes being different than babel, rather than differences in the decorator implementations. That's just a guess though, cause that was the case of previous issues I resolved with TypeScript vs Babel decorators. |
You're right about stage-0 vs stage-2. I misunderstood the document and agree the wording is poor. I also haven't been tracking the changes closely, so I appreciate the info. I'll put a few more licks in on the experiment tonight, but so far biggest change I found needed wsa places where the tests were doing default imports, e.g. One odd thing is running the tests with babble, either sort of import works. I know there's a reason TypeScript implements default imports differently, but only vaguely remember the reason was unpleasant at the time I read it... |
hmm that's odd indeed. Generally the emitted runtime code looks to same for imports/exports AFAICT. I'm guessing it's a type thing maybe? Are you including I'm not super familiar with the namespace feature, so it's unclear whether this includes an actual default export or not https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/chai/index.d.ts |
Ahh, I had included
tsc generates something unsurprising...:
however for:
tsc generates:
Obviously the
There are a number of TypeScript bugs dealing with this, but they seem to have been closed. For example: microsoft/TypeScript#11179. |
Wow they don't check for __esModule interop?? They even emit the __esModule: true code on their exports! That's silly. Your * as chai solution sounds good and has the same effect for both. Thank you! |
@BurtHarris it appears (but I haven't confirmed) that in TS the |
Oh, snap! Seems relevant, but I may have been overoptimistic @jayphelps. The codegen from ts with that enabled still doesn't check the __esModule flag when importing, it still seems to generate:
Pretty sure that still won't work for chai. One other pain-in-the-*** I wanted to run by you has to do with the structure of the output directory (e.g. lib): When I include just the I've bumped my head on this before, but never settled on a solution I think is "good". For now, I think the simplest thing is to tell the TypeScript compiler to output to a An alternative might be to change the |
One other way of addressing the output directory structure issue could be to move |
I opened a TypeScript issue on the __esModule flag: microsoft/TypeScript#16090. |
Very nice!! I see they even responded and even discussed in their latest meeting. |
@jayphelps, I've learned some interesting stuff about build tools that might be applicable. Gulp and Lerna both seem attractive and potentially complementary tools. Lerna addresses having multiple different multiple npm packages in a single repository, coordinating version numbers, etc. Might be a good way of managing having both a TypeScript and Babel builds until the compatibility issues get worked out. While not a master of either, I think I could PR a change to the build process to use them. |
@BurtHarris hmm I definitely don't want to maintain two versions, one in TS and one in babel. 😢 |
@jayphelps to be clear, I was thinking that both versions would be build from the same source code, but I understand the reluctance. |
@BurtHarris what all is left to merge the index.d.ts with the actual source files so we don't need to maintain both? i.e. build from TS which will generate the actual definitions. |
@jayphelps, I'll re-examine at that today. If I remember correctly the tests I had the hardest time with were ones for the deprecated decorators: I've learned a trick or two in the meantime... :-) |
Oh yea, this packages |
@jayphelps, I'm a but unclear on what should go into the |
@BurtHarris the |
@jayphelps You're at the edge of my understanding here, but I'm not sure that TypeScript really supports the combination output you are describing, though I have to admit some troubleshooting on an unrelated issue make it sound like a useful one.) If I use Searching the web for material similar to what you were saying I found this: https://github.com/systemjs/systemjs/blob/master/docs/module-formats.md. I've not looked into SystemJS much, is this what you are referring to? |
@BurtHarris sorry for the delayed response, did you figure it out? Here's an example taken from an internal library I have that does this:
|
Yea, I did. There are some fine points about the differences betwee --target, --module, and --lib I hadn't understood before. Good learning experience. |
I'm working on a branch with the idea of using TypeScript
tsc
to build core-decorators.js and the tests. Progress good, but one issue has me puzzled. When I code likeimport decorators from '../';
I'm getting a node.js error Error: Cannot find module '../'.I'm not sure how this works under babel, I thought there had to be an
index.js
in the directory to make this work.https://github.com/BurtHarris/core-decorators.js/tree/typescriptTests if you want to have a peek.
The text was updated successfully, but these errors were encountered: