This course has been updated! We now recommend you take the TypeScript Fundamentals, v3 course.
Transcript from the "Flags" Lesson
>> Mike North: So I'm just gonna open up, in hello-ts, we've got a source folder and then Index-ts within that folder.
[00:00:41] There's just only one file in that folder. What we've got here is some TypeScript code, and it's just two functions, and then a tiny little program that runs, that uses the above functions. First, we have a function that returns a promise that resolves after a set period of time.
[00:01:55] So we're gonna look at different ways of compiling this so that we can get an output that will run where we need it to work. All right, I'm gonna open up my terminal here. So, and just to be clear, it's important you use Visual Studio Code for this workshop.
>> Mike North: It is the editor that is built kinda hand-in-hand with TypeScript, right next to the team that built TypeScript itself. So it's possible to set up other environments such that you get the kind of experience we're about to see. But it requires much more effort than what you'll see right here.
>> Mike North: All right, so the first thing I'm gonna do,
>> Mike North: I'm gonna go into examples/hello-ts, so I'm sort of working in the hello-ts folder,
>> Mike North: And I'm gonna see what's in the source folder only this one file. So I wanna use this command tsc, which is TypeScript compiler, src/index.ts.
>> Mike North: And what we have here is another file that's popped up, index.js.
>> Mike North: And if we look at these side-by-side, it's a lot of extra code. I mean, if we try to find our original code, it looks like it starts down here,
>> Mike North: And then more kind of explosion of complexity.
>> Mike North: Okay, well, I can see right away a lot of the code went away. Line 31 is where we end. And we still have a little overhead here like some help or code that's necessary for compatibility reasons. But now I'm seeing promise pop up. Promise is a part of ES6 or ES2015.
[00:05:17] Now, if we tried to run this, let's let's try to run this in Node, cuz there's nothing here that's necessarily browser specific in terms of like functions we're calling. So we could just run node src/index.js, and we'll get an error, and that error says unexpected token export. Has anyone here worked with Node before?
[00:05:42] What am I running into here?
>> Mike North: Can you spot a problem that would make Node potentially unhappy?
>> Mike North: Export, can we maybe look, how does Node.js export things?
>> Speaker 2: Module.exports.
>> Mike North: Module.exports, right, and this is because commonjs modules are used for Node. We import things with require, we export things with module that exports.
[00:06:11] So we can add another compiler flag here.
>> Mike North: Modules or module commonjs, and we should see this change, and in fact it does, exports.addnumbers. And now if we will try to run it, everything works, right? We wait for brief period of time and then we log out the result of the adding three and four together.
[00:06:37] So there a lot different flags you can use here. Here's another one of my favorites, --watch. And that will leave the TypeScript compiler watching the source files, so that if I were to change something, like let's say I add a string here, result is,
>> Mike North: And comma, and I'm gonna hit Save and you can see the file on the right kinda updated automatically.
[00:07:04] So it watches your source files and will incrementally update only what's changed. It's a nice, fast, incremental compilation. It doesn't do your whole app, just what's changed.