Hey there, My name is Chris Noring and welcome to
this video on Copilot and TypeScript. I'll take you through
how to build a Web API using TypeScript. And guess
what? I'm actually not going to type code. I'm going
to let Copilot do it for me. Everything from production
code to unit test to documentation. I'm just going to
use prompts. You're going to be amazed. If you're new
to Copilot, check out this video. We're.
Inside a Visual Studio Code.
I have created an app ts file. This is a
very common entry point file for developing anything TypeScript. What
I've done here at the top left is to have
a comment, a prompt where I want to tell copilot
to help me. So I'm saying interface customer with e-mail
and name field. So now I'm going to put myself
on the next Rd. just to see what copilot suggests.
Okay I say I get an interface, I get an
e-mail string, I get a name string.
And there is the end of the interface. This is
fantastic. This is a great beginning. Next up, let's create
our Web API endpoint. Now create that endpoint. We need
to create another comment that we need to use to
instruct Copilot. So I'm saying create Express POST request for
customer. And now let's see what Copilot is telling me
to do at POST. That's great. Request, response, validate customer.
Yes, I want to do that.
Request body. That's perfect. That means that someone is posting
their customer data to me. Validate e-mail. I definitely want
to do that. That's later in this video and here
we can see how if you're not providing a valid
e-mail, it's going to spit out 400 error response. And
we see that it wants to store the customer and
put the customer in a list. Great. And customer created.
Fantastic. It has been able to create all the things
that I needed to create for me and it is
context aware. It knows but customer. However, I don't have
the validate e-mail function. Can Copilot help me even with
that? So validate e-mail function, look at that. I'm getting
an e-mail regex and you can see that these are
the type of regexes pattern matching that I don't want
to write myself. So I let Copilot do it for
me.
It even tries to test out the e-mail. Now of
course, as a very responsible developer, there are a few
things that I need to add to my code to
actually be something that I can put into production. Those
two pieces are about testing because I want to test
all my code and documenting. If I do that too,
I'm going to show up really, really well. There are
many test frameworks out there. One test framework I often
use is Jasmine, so I want to tell Copilot to
create a Jasmine test next.
So I'm typing my prompt validate e-mail test in Jasmine
and I can see how it's giving me the describe,
the IT and the expect and everything is supposed to
be true. So at this point I have everything that
I'm asking for. It even suggests some new test for
me should return false. So let's try to add that
one as well. Great, so now I have a test
that should succeed and I have another test that shouldn't
succeed.
Now, of course you're not going to put all this
code into this one file, right? No, I'm not going
to do that. I'm going to move code about and
you're going to see exactly how I'm going to do
that. So let's do that next. Okay. We have moved
out some code into various files. We have an e-mail
dot ts where the interface and the validate e-mail function
goes. We got a test code that belongs in a
test file. That's great at TS, but you might wonder
where the app dot post comes from. So we're going
to tell.
Pilot about that one too, so it can help me
to create that express application that I'm building. And what
you're seeing here is how I'm creating the Express app
and it's even is adding a GET request for me
as well for customers, which is fantastic. Now it's making
my life even easier. Documentation is one of those things
that belong with a professional developed application. So we're going
to be adding that next and we can type document,
interface, customer and see where it gives us.
We can see here we get interface customer, we get
property string e-mail, name and it gives us even an
example. You can see how that works and this is
probably quite enough. And we see that we have documentation
on the interface. What about the e-mail function? Just like
before it is giving us a function RAM.
What it returns and even an example trying to invoke
set function, that's really it, right? Well, we're not quite
there because right now we can validate an e-mail. That
is great. We have an interface customer that is also
great. We have documented all the parts. So what is
actually missing now let's have a look. This has to
store customers. This is some kind of in memory array
and this is something we can ask Copilot for help
with. So we can just ask it to create that.
So our code checks out.
So we are calling validate e-mail. That's great. We have
an if and else scenario, whether we have a valid
or invalid e-mail and it's able to store the customer
if everything is good and it's able to do everything
we need. There wasn't much code here that we typed
today. In fact, all we did was to provide these
prompts for Copilot to give us what we needed. Missing
here as part of our code is some imports to
make sure that everything works.
I believe even this is something that Copilot can help
us with. The only reason why this doesn't work is
that this isn't part of a module that we have
installed yet, so we still have that installation business to
do. You didn't think that Copilot was just going to
completely replace you, right? You still need to know exactly
what you're doing for this to work. If you know
how things work, what parts goes together, Copilot can definitely
type it faster for you.
Wasn't that amazing? We were able to take you through
building a Web API with nothing but prompts. Copilot has
done 99% of the job. Just imagine how this is
going to make you twice as fast or even faster.
If you've found value out of this, please like and
subscribe.