Skip to main content
Loading Explore guides…
TypeScript for JavaScript · Acedra
Home
Explore
Computer Science
TypeScript for JavaScript
Sign in to save
Chat
If YouTube asks you to sign in, open the video on YouTube.
Watch on YouTube
Reload player
Transcript
6 segments
Copy
Hide video
0:00
[Music] typescript validate your javascript ahead of time with static type checking javascript is a dynamic language where we can do all kinds of crazy things like reference variables that don't exist or work with objects of an unknown shape the code is interpreted by a browser but if your code is broken you won't catch it until runtime when the browser rows an error typescript prevents errors like this from ever happening by extending javascript with types the language is a strict superset of
0:24
javascript which means when you open up a ts file you can write plain javascript with all of its extra features being completely optional but notice in the code here how the ide is providing feedback about using a variable that doesn't exist instead of fixing this issue weeks later in a stack trace after the company has lost millions of dollars we can fix it right here right now the reason we get this instant feedback is because typescript behaves like a compiled language
0:47
where javascript is the compilation target you can run the typescript compiler using the tsc command it will take the ts file and transpile it into vanilla javascript and you can choose any flavor of javascript you want if you need to target ancient browsers and that means you can use the latest and greatest syntax features of javascript without having to worry if they'll be supported in an older environment your typescript project will likely have a ts config file
1:11
which provides an infinite number of ways to customize the behavior of the compiler but the primary goal of typescript is to enable static typing one way it achieves that is by allowing you to annotate your code with types we can strongly type a variable using a colon followed by its type like a string boolean or number that's known as an explicit type if we then try to assign its value as the wrong type we get an error alternatively if we set an initial value it will implicitly infer the type
1:37
however there may be cases where you want to opt out of this behavior in which case you can annotate with the any type that allows you to loosely type or opt out of type checking when working with an array use brackets to strongly type a list in addition you can define your own custom types and interfaces which is especially powerful when working with objects the car interface here defines various types of the properties on an object we can then apply the interface to a plain javascript object
2:03
the beauty of having strongly typed code is that we get autocomplete everywhere in our ide we don't have to jump back and forth to documentation or dig through stack traces to figure out why our code's not working if you like typescript as much as i do consider becoming a pro member at fireship io for the next few days everything will be 40 off including lifetime memberships thanks for watching and i will see you in the next one
Here to help you learn
Sign in to keep a private conversation with your course tutor.
Ask me anything about the material…
Sign in to chat