Upgrade to Pro — share decks privately, control downloads, hide ads and more …

TypeScript for C++ Programmers

TypeScript for C++ Programmers

First appearing in 2012, TypeScript has grown a lot in the past 9 years and looking at how the language operates is a great exercise for C++ programmers. We will go over basic usage and compare it to similar functionality in C++, explore the type system the language offers, look at the environment the language lives in and it's relation to JavaScript and then we will look at the development tooling that is available. This talk will hopefully teach you a few new things about the language and maybe it will open some doors in the future regarding language choices or how you approach problem-solving.

Ólafur Waage

October 21, 2021
Tweet

More Decks by Ólafur Waage

Other Decks in Programming

Transcript

  1. 3

  2. “ You can’t always stay in your corner waiting for

    others to come to you. You sometimes have to go to them. - Winnie the Pooh 4
  3. “ You can’t always stay in your corner waiting for

    others to come to you. You sometimes have to go to them. - Winnie the Pooh 5
  4. WHAT THIS TALK IS AND ISN’T Going over a language

    requires a lot of items to be covered. There are always things you can miss or not cover in detail 6
  5. WHAT THIS TALK IS AND ISN’T Going over a language

    requires a lot of items to be covered. There are always things you can miss or not cover in detail 7 This is not a comprehensive overview of the language, but more a curious look into this world
  6. WHAT THIS TALK IS AND ISN’T Going over a language

    requires a lot of items to be covered. There are always things you can miss or not cover in detail 8 This is not a comprehensive overview of the language, but more a curious look into this world The point of view is from a C++ programmer but anyone not familiar with this world can benefit from this talk
  7. 9

  8. IT ALL STARTED WITH THE WORLD WIDE WEB 14 BROWSERS

    It started with Netscape adding JavaScript in 1995
  9. IT ALL STARTED WITH THE WORLD WIDE WEB 15 BROWSERS

    It started with Netscape adding JavaScript in 1995 (we almost got Java or Scheme)
  10. IT ALL STARTED WITH THE WORLD WIDE WEB 16 BROWSERS

    It started with Netscape adding JavaScript in 1995 (we almost got Java or Scheme) Then we get ECMAScript in 1997 which serves as the JavaScript standard
  11. IT’S ALL GOOGLE’S FAULT 18 CHROME Then in 2008 the

    Chromium project introduces V8 as a JavaScript engine.
  12. IT’S ALL GOOGLE’S FAULT 19 CHROME Then in 2008 the

    Chromium project introduces V8 as a JavaScript engine. This made the language much more performant
  13. WAIT WE CAN DO THIS OFFLINE? 21 NODE.JS Then in

    2009 Node.js arrived which used this V8 engine to run JavaScript code without a browser.
  14. WAIT WE CAN DO THIS OFFLINE? 22 NODE.JS Then in

    2009 Node.js arrived which used this V8 engine to run JavaScript code without a browser. The stack is becoming full.
  15. THE LIBRARY OF JAVASCRIPTIA 24 NPM And then in 2010

    we get npm as package manager for libraries that are designed to run within the Node.js ecosystem.
  16. THE LIBRARY OF JAVASCRIPTIA 25 NPM And then in 2010

    we get npm as package manager for libraries that are designed to run within the Node.js ecosystem. The world at your fingertips
  17. A GOOD FOUNDATION These projects then formed the basis of

    the JavaScript development environment. 26
  18. A GOOD FOUNDATION These projects then formed the basis of

    the JavaScript development environment. (there are other tools but the ideas are the same) 27
  19. JAVASCRIPT IS NOT PERFECT Now we have this environment. And

    a lot of people start building all sorts of applications with only JavaScript 28
  20. JAVASCRIPT IS NOT PERFECT Now we have this environment. And

    a lot of people start building all sorts of applications with only JavaScript 29 The language has its flaws (WAT), and as far back as 2002 there existed static analysis tools to catch common errors
  21. JAVASCRIPT IS NOT PERFECT Now we have this environment. And

    a lot of people start building all sorts of applications with only JavaScript 30 The language has its flaws (WAT), and as far back as 2002 there existed static analysis tools to catch common errors JavaScript’s type system is dynamic. There are underlying primitives but also the all mighty (JSON) object.
  22. IT’S ALL ABOUT JAVASCRIPT Before we go into TypeScript, we

    need to have a short chat about JavaScript. 31
  23. IT’S ALL ABOUT JAVASCRIPT Before we go into TypeScript, we

    need to have a short chat about JavaScript. Let’s look at a few simple examples of the language, enough to get a sense of why TypeScript exists in the first place. 32
  24. IT’S ALL ABOUT JAVASCRIPT Before we go into TypeScript, we

    need to have a short chat about JavaScript. Let’s look at a few simple examples of the language, enough to get a sense of why TypeScript exists in the first place. This will give you some context going forwards in the talk. 33
  25. TYPESCRIPT FROM A DISTANCE Many had looked at “The JavaScript

    Problem” and tried to solve it by creating a new programming language 51
  26. TYPESCRIPT FROM A DISTANCE Many had looked at “The JavaScript

    Problem” and tried to solve it by creating a new programming language 52 TypeScript looked at this problem by becoming a strict superset of JavaScript All JavaScript is legal TypeScript
  27. TYPESCRIPT FROM A DISTANCE Many had looked at “The JavaScript

    Problem” and tried to solve it by creating a new programming language 53 TypeScript looked at this problem by becoming a strict superset of JavaScript All JavaScript is legal TypeScript TypeScript is then compiled into JavaScript At runtime TypeScript does not exist
  28. 54

  29. TYPESCRIPT BASICS Now let’s shift the focus to TypeScript the

    language itself. Some of these features come directly from or are based on JavaScript but it’s easier to think of TypeScript as a whole than to keep going back and forwards. 69
  30. TYPESCRIPT HISTORY Released in 2012 and developed by Microsoft. The

    idea of TypeScript was to add type safety to JavaScript but also have existing JavaScript applications be valid TypeScript applications. 71
  31. TYPESCRIPT HISTORY Released in 2012 and developed by Microsoft. The

    idea of TypeScript was to add type safety to JavaScript but also have existing JavaScript applications be valid TypeScript applications. Applications are compiled into JS files and the language also has a language server for syntax highlighting and error checking. 72
  32. THE TYPES OF TYPESCRIPT One thing to note here, because

    JavaScript is TypeScript and TypeScript does not exist at runtime, there are no TypeScript specific containers* 95
  33. THE TYPES OF TYPESCRIPT One thing to note here, because

    JavaScript is TypeScript and TypeScript does not exist at runtime, there are no TypeScript specific containers* They are all JavaScript containers (array, map, set, etc) 96
  34. THE TYPES OF TYPESCRIPT One thing to note here, because

    JavaScript is TypeScript and TypeScript does not exist at runtime, there are no TypeScript specific containers* They are all JavaScript containers (array, map, set, etc) Many of the algorithms we know exist within the containers themselves. 97
  35. “ADVANCED” TYPESCRIPT So now we have seen an overview of

    the basic features of the language, and most things are behaving as we would expect. 98
  36. “ADVANCED” TYPESCRIPT So now we have seen an overview of

    the basic features of the language, and most things are behaving as we would expect. But now let’s take a tour into the the type system of TypeScript. 99
  37. STRUCTURAL TYPE SYSTEM TypeScript uses a structural type system, or

    “type compatibility” as they call it. 100
  38. STRUCTURAL TYPE SYSTEM TypeScript uses a structural type system, or

    “type compatibility” as they call it. Structural typing is taking the “looks like a duck, quacks like a duck” idea and taking it quite literally. 101
  39. STRUCTURAL TYPE SYSTEM TypeScript uses a structural type system, or

    “type compatibility” as they call it. Structural typing is taking the “looks like a duck, quacks like a duck” idea and taking it quite literally. But TypeScript does sacrifice a bit of what is called “soundness”. 102
  40. STRUCTURAL TYPE SYSTEM TypeScript uses a structural type system, or

    “type compatibility” as they call it. Structural typing is taking the “looks like a duck, quacks like a duck” idea and taking it quite literally. But TypeScript does sacrifice a bit of what is called “soundness”. It’s best to see this system with examples. 103
  41. CONST Let’s talk about const real quick, because how it

    functions in TypeScript and JavaScript is different from C++ 126
  42. CONST Let’s talk about const real quick, because how it

    functions in TypeScript and JavaScript is different from C++ The language has features that will get us similar results though 127
  43. CONST Let’s talk about const real quick, because how it

    functions in TypeScript and JavaScript is different from C++ The language has features that will get us similar results though But those results might behave a bit differently 128
  44. UTILITY TYPES TypeScript has a collection of Utility Types. These

    are generic classes that provide specific features. 139
  45. UTILITY TYPES TypeScript has a collection of Utility Types. These

    are generic classes that provide specific features. Most of these types will take your type as T and transform it into a new type with some new feature or functionality. 140
  46. 148

  47. 149

  48. 150

  49. 151

  50. 152

  51. 153

  52. 154

  53. 155

  54. 156

  55. DECLARATION FILES There are a lot of JavaScript libraries out

    there, and they can be installed using npm and used within TypeScript. But TypeScript also wants you to use those libraries safely. 157
  56. DECLARATION FILES There are a lot of JavaScript libraries out

    there, and they can be installed using npm and used within TypeScript. But TypeScript also wants you to use those libraries safely. A TypeScript declaration file (.d.ts) describes the exported interface of the JavaScript library, but (possibly) with a safer definition. 158
  57. 159

  58. 160

  59. 161

  60. SUMMARY In this talk we have gone over a history

    of JavaScript. What environment it uses and the problems exist within the language. 163
  61. SUMMARY In this talk we have gone over a history

    of JavaScript. What environment it uses and the problems exist within the language. We have seen what problems TypeScript is trying to solve and its relationship with JS 164
  62. SUMMARY We have seen TypeScript features, like Classes and Functions.

    We have gone over how const works and some Utility Types 166
  63. SUMMARY We have seen TypeScript features, like Classes and Functions.

    We have gone over how const works and some Utility Types And we have looked at the structural type system and declaration files 167
  64. SUMMARY So let’s think about this for a minute. We

    have: - An old standards driven language that has perceived complexity and quirks 171
  65. SUMMARY So let’s think about this for a minute. We

    have: - An old standards driven language that has perceived complexity and quirks - Attempts to fix it with other languages or static analysis 172
  66. SUMMARY So let’s think about this for a minute. We

    have: - An old standards driven language that has perceived complexity and quirks - Attempts to fix it with other languages or static analysis - An immense backlog of libraries and backwards compatibility concerns 173
  67. SUMMARY So let’s think about this for a minute. We

    have: - An old standards driven language that has perceived complexity and quirks - Attempts to fix it with other languages or static analysis - An immense backlog of libraries and backwards compatibility concerns - “Headers” towards the old language to increase safety and to allow usage of libraries from the old language 174
  68. SUMMARY So let’s think about this for a minute. We

    have: - An old standards driven language that has perceived complexity and quirks - Attempts to fix it with other languages or static analysis - An immense backlog of libraries and backwards compatibility concerns - “Headers” towards the old language to increase safety and to allow usage of libraries from the old language - Old language has features that are not desired in new language, so methods exist to replace them with other features or options to turn them off (or warn for them) 175
  69. 176

  70. SUMMARY C++ can learn a lot from what is happening

    here. Not only with regards to its relationship with C, but going forwards. 178
  71. SUMMARY C++ can learn a lot from what is happening

    here. Not only with regards to its relationship with C, but going forwards. We have changed the language a lot in the past decade or so. 179
  72. SUMMARY C++ can learn a lot from what is happening

    here. Not only with regards to its relationship with C, but going forwards. We have changed the language a lot in the past decade or so. We even have improved our tooling a lot to help deal with many of the quirks of the language. 180
  73. SUMMARY But some of the things we have in C++

    are probably never going away, or we’re probably never going to stop having certain quirks. 181
  74. SUMMARY But some of the things we have in C++

    are probably never going away, or we’re probably never going to stop having certain quirks. Could you imagine a language that was on top of C++, let’s call it C+=2 or something. Where you write in a language that is then turned into C++. 182
  75. SUMMARY But some of the things we have in C++

    are probably never going away, or we’re probably never going to stop having certain quirks. Could you imagine a language that was on top of C++, let’s call it C+=2 or something. Where you write in a language that is then turned into C++. But you could then turn off certain language features or structure the language where older (previously idiomatic code) would no longer be allowed. Just an idea. 183