Fireship · @fireship

TypeScript in 100 Seconds

Uploaded November 25, 2020 · Length 2:25

TypeScript in 100 Seconds

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.

  1. 0:00

    [Music]

  2. 0:00

    typescript validate your javascript

  3. 0:02

    ahead of time with static type checking

  4. 0:04

    javascript is a dynamic language where

  5. 0:06

    we can do all kinds of crazy things

  6. 0:08

    like reference variables that don't

  7. 0:09

    exist or work with objects of an unknown

  8. 0:11

    shape the code is interpreted by a

  9. 0:13

    browser

  10. 0:13

    but if your code is broken you won't

  11. 0:15

    catch it until runtime when the browser

  12. 0:17

    rows an error

  13. 0:18

    typescript prevents errors like this

  14. 0:19

    from ever happening by extending

  15. 0:21

    javascript with types

  16. 0:22

    the language is a strict superset of

  17. 0:24

    javascript which means when you open up

  18. 0:26

    a ts file you can write plain javascript

  19. 0:28

    with all of its extra features

  20. 0:30

    being completely optional but notice in

  21. 0:31

    the code here how the ide

  22. 0:33

    is providing feedback about using a

  23. 0:35

    variable that doesn't exist

  24. 0:36

    instead of fixing this issue weeks later

  25. 0:38

    in a stack trace after the company has

  26. 0:40

    lost millions of dollars we can fix it

  27. 0:41

    right here right now

  28. 0:43

    the reason we get this instant feedback

  29. 0:45

    is because typescript behaves like a

  30. 0:46

    compiled language

  31. 0:47

    where javascript is the compilation

  32. 0:49

    target you can run the typescript

  33. 0:51

    compiler

  34. 0:52

    using the tsc command it will take the

  35. 0:54

    ts file and transpile it into vanilla

  36. 0:56

    javascript

  37. 0:57

    and you can choose any flavor of

  38. 0:58

    javascript you want if you need to

  39. 0:59

    target ancient browsers

  40. 1:01

    and that means you can use the latest

  41. 1:02

    and greatest syntax features of

  42. 1:04

    javascript without having to worry if

  43. 1:06

    they'll be supported in an older

  44. 1:07

    environment your typescript project will

  45. 1:09

    likely have a ts config file

  46. 1:11

    which provides an infinite number of

  47. 1:12

    ways to customize the behavior of the

  48. 1:14

    compiler

  49. 1:15

    but the primary goal of typescript is to

  50. 1:17

    enable static typing

  51. 1:18

    one way it achieves that is by allowing

  52. 1:20

    you to annotate your code with types

  53. 1:22

    we can strongly type a variable using a

  54. 1:24

    colon followed by its type like a string

  55. 1:26

    boolean or number

  56. 1:27

    that's known as an explicit type if we

  57. 1:29

    then try to assign its value as the

  58. 1:31

    wrong type we get an error

  59. 1:33

    alternatively if we set an initial value

  60. 1:35

    it will implicitly infer the type

  61. 1:37

    however there may be cases where you

  62. 1:38

    want to opt out of this behavior

  63. 1:40

    in which case you can annotate with the

  64. 1:42

    any type that allows you to loosely type

  65. 1:44

    or opt out of type checking when working

  66. 1:46

    with an array

  67. 1:47

    use brackets to strongly type a list in

  68. 1:50

    addition you can define your own custom

  69. 1:51

    types and interfaces

  70. 1:53

    which is especially powerful when

  71. 1:54

    working with objects the car interface

  72. 1:56

    here defines various types

  73. 1:58

    of the properties on an object we can