Fireship · @fireship
TypeScript in 100 Seconds
Uploaded November 25, 2020 · Length 2:25
Summary
typescript tutorial for beginners: static type checking and compilation
- typescript tutorial
- javascript static type checking
- typescript compilation
- typescript for beginners
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
[Music]
- 0:00
typescript validate your javascript
- 0:02
ahead of time with static type checking
- 0:04
javascript is a dynamic language where
- 0:06
we can do all kinds of crazy things
- 0:08
like reference variables that don't
- 0:09
exist or work with objects of an unknown
- 0:11
shape the code is interpreted by a
- 0:13
browser
- 0:13
but if your code is broken you won't
- 0:15
catch it until runtime when the browser
- 0:17
rows an error
- 0:18
typescript prevents errors like this
- 0:19
from ever happening by extending
- 0:21
javascript with types
- 0:22
the language is a strict superset of
- 0:24
javascript which means when you open up
- 0:26
a ts file you can write plain javascript
- 0:28
with all of its extra features
- 0:30
being completely optional but notice in
- 0:31
the code here how the ide
- 0:33
is providing feedback about using a
- 0:35
variable that doesn't exist
- 0:36
instead of fixing this issue weeks later
- 0:38
in a stack trace after the company has
- 0:40
lost millions of dollars we can fix it
- 0:41
right here right now
- 0:43
the reason we get this instant feedback
- 0:45
is because typescript behaves like a
- 0:46
compiled language
- 0:47
where javascript is the compilation
- 0:49
target you can run the typescript
- 0:51
compiler
- 0:52
using the tsc command it will take the
- 0:54
ts file and transpile it into vanilla
- 0:56
javascript
- 0:57
and you can choose any flavor of
- 0:58
javascript you want if you need to
- 0:59
target ancient browsers
- 1:01
and that means you can use the latest
- 1:02
and greatest syntax features of
- 1:04
javascript without having to worry if
- 1:06
they'll be supported in an older
- 1:07
environment your typescript project will
- 1:09
likely have a ts config file
- 1:11
which provides an infinite number of
- 1:12
ways to customize the behavior of the
- 1:14
compiler
- 1:15
but the primary goal of typescript is to
- 1:17
enable static typing
- 1:18
one way it achieves that is by allowing
- 1:20
you to annotate your code with types
- 1:22
we can strongly type a variable using a
- 1:24
colon followed by its type like a string
- 1:26
boolean or number
- 1:27
that's known as an explicit type if we
- 1:29
then try to assign its value as the
- 1:31
wrong type we get an error
- 1:33
alternatively if we set an initial value
- 1:35
it will implicitly infer the type
- 1:37
however there may be cases where you
- 1:38
want to opt out of this behavior
- 1:40
in which case you can annotate with the
- 1:42
any type that allows you to loosely type
- 1:44
or opt out of type checking when working
- 1:46
with an array
- 1:47
use brackets to strongly type a list in
- 1:50
addition you can define your own custom
- 1:51
types and interfaces
- 1:53
which is especially powerful when
- 1:54
working with objects the car interface
- 1:56
here defines various types
- 1:58
of the properties on an object we can


