Fireship · @fireship

Short Imports with TypeScript Path Mapping

Uploaded January 6, 2018 · Length 4:32

Short Imports with TypeScript Path Mapping

Summary

typescript path mapping for angular projects to eliminate long relative paths

  • typescript path mapping
  • angular productivity booster
  • relative paths
  • typescript namespace
  • angular module imports

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    I need to start this video off on a sad

  2. 0:04

    note on New Year's Day I lost my father

  3. 0:07

    unexpectedly at the age of 57 he lived a

  4. 0:10

    good full life but I just want to remind

  5. 0:12

    everybody to enjoy the time you have

  6. 0:13

    with your family because it can end

  7. 0:15

    before you know it I'd like to say thank

  8. 0:17

    you to everybody who supports this

  9. 0:19

    project and I look forward to bringing

  10. 0:21

    you a whole bunch of awesome resources

  11. 0:22

    in 2018 to kick things off I'm going to

  12. 0:25

    give you a quick angular productivity

  13. 0:27

    booster to eliminate long relative paths

  14. 0:29

    that look like this to a single global

  15. 0:32

    namespace this means you can give all of

  16. 0:34

    your feature modules a specific

  17. 0:36

    namespace and you never really have to

  18. 0:38

    worry about relative paths within your

  19. 0:39

    project to demonstrate this I'm starting

  20. 0:42

    off in a brand new angular 5 app and I'm

  21. 0:45

    going to generate a couple of ng modules

  22. 0:47

    first one being Corps second one being

  23. 0:50

    shared from there I'm going to generate

  24. 0:53

    a service inside of the core module

  25. 0:55

    we'll just go ahead and call this the

  26. 0:56

    off service then I'm going to generate a

  27. 0:59

    component but for no reason at all I'm

  28. 1:01

    going to nest this deeply within the

  29. 1:03

    file structure of the project let's go

  30. 1:06

    ahead and call it the deeply nested user

  31. 1:08

    login component then if we look at the

  32. 1:11

    file structure on the left you can see

  33. 1:12

    the actual component file itself is

  34. 1:15

    nested about six levels deep within our

  35. 1:17

    project this isn't really a problem

  36. 1:19

    unless we need to import this file in a

  37. 1:22

    different file that's nested higher up

  38. 1:24

    so let's try this by importing the

  39. 1:26

    service into the component just a quick

  40. 1:28

    side tip here whenever you import a file

  41. 1:30

    in typescript start with empty

  42. 1:32

    parentheses then type out the path so

  43. 1:34

    you can use typescript autocomplete from

  44. 1:36

    your IDE and save yourself a few extra

  45. 1:38

    characters of typing at this point we're

  46. 1:42

    going to have to traverse up the file

  47. 1:44

    structure so we do dot dot slash a whole

  48. 1:46

    bunch of times until we finally get to

  49. 1:48

    our core module then we go in there and

  50. 1:50

    grab the off service if you're an

  51. 1:53

    angular developer you probably get

  52. 1:55

    really annoyed if you have to do this

  53. 1:56

    frequently in a large complex file

  54. 1:58

    structure it also becomes a problem if