{"id":3728,"date":"2024-09-06T08:48:36","date_gmt":"2024-09-06T13:48:36","guid":{"rendered":"https:\/\/frontendmasters.com\/blog\/?p=3728"},"modified":"2024-09-06T08:48:36","modified_gmt":"2024-09-06T13:48:36","slug":"a-complete-guide-to-beginning-with-typescript","status":"publish","type":"post","link":"https:\/\/frontendmasters.com\/blog\/a-complete-guide-to-beginning-with-typescript\/","title":{"rendered":"A Complete Guide to Beginning with TypeScript"},"content":{"rendered":"\n<p>This guide is not itself the means to learn TypeScript. This guide addresses issues around getting started (obstacles, preliminary knowledge, etc.) and then points you to curated resources to start learning. Consider this material a prologue or an introduction to the learning process itself so, you have the needed background and context before beginning the learning process.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"prerequisite-knowledge\">Prerequisite Knowledge<\/h2>\n\n\n\n<p>The list below briefly highlights the knowledge needed before learning TypeScript:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An intermediate understanding of JavaScript, especially the nature of JavaScript&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Guide\/Grammar_and_Types#data_types\">data types<\/a>&nbsp;and the difference between pass-by-value vs. pass-by-reference, is&nbsp;<strong>required<\/strong>.<\/li>\n\n\n\n<li>A basic understanding of <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Data_structures#dynamic_and_weak_typing\">the difference between dynamically typed languages vs. statically typed languages and weakly typed languages vs. strongly typed languages<\/a>&nbsp;in the context of JavaScript.<\/li>\n\n\n\n<li>The TypeScript compiler is a Node.js CLI tool that takes in TypeScript and outputs JavaScript. One will need a working knowledge of Node.js, npm, and packages. While one can circumvent this learning curve by starting with the&nbsp;<a href=\"https:\/\/www.typescriptlang.org\/play\">TypeScript Playground<\/a>, eventually, you\u2019ll want to install and use the TypeScript compiler on your local machine via Node.js. Thus, a working understanding of Node.js, npm, and command line tools is needed to run TypeScript on your local computer.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"helpful-background-on-typescript\">Helpful Background on TypeScript<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>TypeScript is a free to use open-source tool maintained by Microsoft. The source can be found on&nbsp;<a href=\"https:\/\/github.com\/microsoft\/TypeScript\">GitHub<\/a>.<\/li>\n\n\n\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Data_structures#dynamic_and_weak_typing\">JavaScript is a dynamically typed language<\/a>; in simple terms, this means the language allows you to switch value types after definition&nbsp;(aka re-assignment). In statically typed languages assigning a string value to a variable and then re-assigning the value to a be a number value will throw an error. JavaScript does not throw an error when this type of runtime data value switching occurs. JavaScript dynamically re-assigns the type.&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Data_structures#type_coercion\">JavaScript is also weakly typed<\/a>, meaning that JavaScript can convert values depending on how they are used. For example, if you try and add a string to a number, JavaScript will not throw an error it will convert the number to a string and then combine the strings. For example, in JavaScript <code>\"1\"+1<\/code> becomes <code>11<\/code> because JavaScript converts the number 1 to a string and then combines <code>\"1\" + \"1\"<\/code>, which is <code>\"11\"<\/code>. TypeScript aims to change\/eliminate both the dynamic and weakly typed nature of JavaScript just described.<\/li>\n\n\n\n<li>TypeScript is not one thing. TypeScript is a combination of the following three foundational things:\n<ul class=\"wp-block-list\">\n<li><strong>TypeScript<\/strong>&nbsp;<strong>Type System:<\/strong>&nbsp;A syntax layered over the top of JavaScript for the purpose of making JavaScript a statically\/strongly typed language (aka \u201cTypeScript Types\u201d or \u201cTypeScript Type System\u201d).<\/li>\n\n\n\n<li><strong>TypeScript Compiler:<\/strong>&nbsp;A tool that both 1. interrupts (aka type checking) TypeScript syntax added to JavaScript as well as 2. converts TypeScript to JavaScript (aka the \u201cTypeScript Compiler\u201d or \u201ctsc\u201d). The compiler is provided as a Node.js&nbsp;<a href=\"https:\/\/www.npmjs.com\/package\/typescript\">npm package<\/a>&nbsp;which provides the \u201ctsc\u201d cli tool.<\/li>\n\n\n\n<li><strong>TypeScript<\/strong>&nbsp;<strong>Language Service:<\/strong>&nbsp;The TypeScript language service used by things like VSCode for analyzing TypeScript code as you type it (aka&nbsp;<a href=\"https:\/\/code.visualstudio.com\/docs\/typescript\/typescript-editing\">IntelliSens<\/a><a href=\"https:\/\/code.visualstudio.com\/docs\/typescript\/typescript-tutorial#_intellisense\">e<\/a>, note that the&nbsp;<a href=\"https:\/\/code.visualstudio.com\/docs\/typescript\/typescript-compiling#_compiler-versus-language-service\">compiler is separate from the language service<\/a>)<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>TypeScript is incredibly popular amongst professional developers. If you want to be a professional web developer or Node.js developer, it will likely be the case today that you will need to know TypeScript.<\/li>\n\n\n\n<li>TypeScript itself is not sent to a JavaScript runtime. TypeScript must be converted or built by a compiler to standard ECMAScript before it can be run by a JavaScript engine within a host environment (e.g., web browsers and Node.js). The job of transforming TypeScript to JavaScript is handled by the TypeScript compiler.<\/li>\n\n\n\n<li>Anything that is standard JavaScript is valid TypeScript (i.e., TypeScript is a superset of JavaScript). When you are writing TypeScript-specific syntax (e.g., Type Annotations) you are using syntax that is not standard JavaScript (i.e., not in the ECMAScript specification). While TypeScript can be converted to valid JavaScript, TypeScript itself is not valid JavaScript. If a JavaScript runtime executes TypeScript, it will produce a runtime error because, again, TypeScript is not valid JavaScript.<\/li>\n\n\n\n<li>TypeScript promises that it will never change the behavior of your program; it will only ever remove itself from your JavaScript.<\/li>\n\n\n\n<li>One should consider that adopting TypeScript is signing on to a subjective and stricter layer of control beyond JavaScript standards. This means that TypeScript requires not only valid JavaScript but also valid TypeScript. In this sense, TypeScript is another layer of control guiding how one should write JavaScript (e.g., TypeScript aims to change the fact that JavaScript has dynamic types and is weakly typed). When Typescript is not valid, you get an error in addition to the potential errors that come from invalid JavaScript.<\/li>\n\n\n\n<li>TypeScript is currently in a class of its own. Flow was a similar solution from the past, but it is less popular today. Most developers seeking to write JavaScript and use type annotations use TypeScript. The alternative to solutions like TypeScript and Flow entails abandoning JavaScript for a custom language with a built-in type system that compiles to JavaScript (e.g., Elm, PureScript, ClojureScript, etc.). Compared to languages with built-in type systems, TypeScript is a bolted-on solution to JavaScript, not a new language itself.<\/li>\n\n\n\n<li>TypeScript can also be used to compile today\u2019s ECMAScript to older versions of ECMAScript (e.g., ES 2023 to EE5). This aspect of TypeScript overlaps with tools like&nbsp;<a href=\"https:\/\/babeljs.io\/\">Babel<\/a>,&nbsp;<a href=\"https:\/\/swc.rs\/\">SWC<\/a>,&nbsp;<a href=\"https:\/\/github.com\/evanw\/esbuild\">esbuild<\/a>, and&nbsp;<a href=\"https:\/\/github.com\/alangpierce\/sucrase#sucrase\/\">Surcase<\/a>. Note that it\u2019s possible to use TypeScript for its type engine and then hand off compiling to tools like&nbsp;<a href=\"https:\/\/babeljs.io\/\">Babel<\/a>,&nbsp;<a href=\"https:\/\/swc.rs\/\">SWC<\/a>,&nbsp;<a href=\"https:\/\/github.com\/evanw\/esbuild\">esbuild<\/a>, and&nbsp;<a href=\"https:\/\/github.com\/alangpierce\/sucrase#sucrase\/\">Surcase<\/a>&nbsp;or use TypeScript alone for both ECMAScript conversion and its type-checking engine.<\/li>\n\n\n\n<li>Similar to&nbsp;<a href=\"https:\/\/babeljs.io\/\">Babel<\/a>,&nbsp;<a href=\"https:\/\/swc.rs\/\">SWC<\/a>,&nbsp;<a href=\"https:\/\/github.com\/evanw\/esbuild\">esbuild<\/a>, and&nbsp;<a href=\"https:\/\/github.com\/alangpierce\/sucrase#sucrase\/\">Surcase<\/a>, TypeScript can compile JavaScript from&nbsp;<a href=\"https:\/\/github.com\/tc39\/proposals\">staged ECMAScript proposals<\/a>&nbsp;that have not yet been officially added to ECMAScript (i.e. make use of JavaScript code before it becomes part of JavaScript). This allows you to use new futuristic non-standard changes to the ECMAScript language today.<\/li>\n\n\n\n<li>JavaScript files are typically named with a file extension of \u201c.js\u201d. TypeScript files are typically named with a file extension of \u201c.ts\u201d. If you are using JSX in \u201c.ts\u201d files, these files will have the file extension \u201c.tsx\u201d.<\/li>\n\n\n\n<li>While not necessarily true in the past, as of today TypeScript aims to be compliant with the direction of ECMAScript. Setting aside type annotations, today TypeScript does not try and introduce non-standard JavaScript syntax that has zero paths to potentially being adopted by JavaScript.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"why-learnuse-typescript\">Why Learn\/Use TypeScript?<\/h2>\n\n\n\n<p>Today, most developers who professionally use JavaScript to build software use TypeScript. They do so for the following reasons: By learning TypeScript and learning it well, you\u2019ll better understand the complicated parts of JavaScript, which will make you a better JavaScript developer.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It is not uncommon that employers today require or prefer the use of TypeScript to maintain large repositories of JavaScript code. (i.e., to compete in the job market today, you need TypeScript knowledge).<\/li>\n\n\n\n<li>By using a strongly typed version of JavaScript with clearly defined guard rails, larger teams can enforce strict coding practices that can safeguard the code against less experienced developers.<\/li>\n\n\n\n<li>The authoring experience through tooling is a superior experience to authoring plain JavaScript. (e.g.,&nbsp;<a href=\"https:\/\/code.visualstudio.com\/docs\/languages\/typescript\">VS Code offers an enhanced developer experience when using TypeScript<\/a>).<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"getting-started-with-typescript-obstacles\">Getting Started with TypeScript Obstacles<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Steep Learning Curve<\/h3>\n\n\n\n<p>Adding on TypeScript can be extremely overwhelming both academically and in practice. This is especially true for those just learning JavaScript as well as those who are seasoned JavaScript developers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">More Rules, More Errors<\/h3>\n\n\n\n<p>TypeScript adds additional rules to the JavaScript language regarding data types and will throw a TypeScript error when JavaScript does not. This can be a bit confusing and frustrating. It can\u2019t be stated enough that TypeScript is bossy and a bit messy, especially if TypeScript expects that all values avoid the&nbsp;<a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/2\/everyday-types.html#any\"><code>any<\/code><\/a>&nbsp;type.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">More Code, More Development Time\/Management<\/h3>\n\n\n\n<p>TypeScript type annotations are nonstandard syntactical overhead. It adds more lines of code to your files (i.e., type annotations) and more files (i.e., type declarations) to your code base. It is simply more information to take in and manage. Given a large code base authored and maintained by developers of differing skills, this may be ideal. But it does have a cost. Grokking TypeScript and TypeScript annotations and appeasing the TypeScript compiler can be frustrating, daunting, and time-consuming.<\/p>\n\n\n\n<p>The fact is, that TypeScript takes more time and adds lines of complexity. Many teams believe the value provided to be worth the overhead. The cost of implementation vs. the value provided is a complicated equation with many variables. How developers evaluate this equation often will say more about the developers than it says objectively about code quality, code durability, or code longevity.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">A Compiler Step<\/h3>\n\n\n\n<p>While most professionals are already accustomed to the tooling\/building processes that modern JavaScript development requires TypeScript is a far cry from simply writing some code that is executed by a web browser by way of a script element. TypeScript comes with the overhead of compiling it to standard JavaScript.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"top-use-cases-of-typescript\">Top Use Cases of TypeScript<\/h2>\n\n\n\n<p>The following four use cases for TypeScript overlap and extend the reasons why a developer might choose to learn TypeScript:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><a href=\"#typescripts-type-system-brings-strongly-typed-enforceable-mechanics-to-javascript\">Typescripts Type System Brings Strongly Typed Enforceable Mechanics to JavaScript<\/a><\/strong><\/li>\n\n\n\n<li><strong><a href=\"#typescript-reports-javascript-syntax-errors-as-well-as-typescript-type-errors\">TypeScript Reports JavaScript Syntax Errors as well as TypeScript Type Errors<\/a><\/strong><\/li>\n\n\n\n<li><strong><a href=\"#typescript-vs-code-provide-a-superior-authoring-environment-for-javascript\">TypeScript &amp; VS Code Provide a Superior Authoring Environment for JavaScript<\/a><\/strong><\/li>\n\n\n\n<li><strong><a href=\"#typescript-can-compiling-current-and-next-generation-ecmascript-to-older-ecmascript\">TypeScript Can Compile Current and Next Generation ECMAScript to Older ECMAScript<\/a><\/strong><\/li>\n<\/ul>\n\n\n\n<p>Let\u2019s examine each of these use cases in more detail.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Typescripts Type System Brings Strongly Typed Enforceable Mechanics to JavaScript<\/h3>\n\n\n\n<p>TypeScript is most well-known for its type system. I\u2019ll briefly explain the tenants and implications of this system so that one can minimally begin to understand its value.<\/p>\n\n\n\n<p>To review, the&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Guide\/Grammar_and_Types#data_types\">JavaScript language has eight types of values that can be used by the language<\/a>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>Boolean<\/code><\/li>\n\n\n\n<li><code>null<\/code><\/li>\n\n\n\n<li><code>undefined<\/code><\/li>\n\n\n\n<li><code>Number<\/code><\/li>\n\n\n\n<li><code>BigInt<\/code><\/li>\n\n\n\n<li><code>String<\/code><\/li>\n\n\n\n<li><code>Symbol<\/code><\/li>\n\n\n\n<li><code>Object<\/code><\/li>\n<\/ol>\n\n\n\n<p>Behind the scenes, JavaScript dynamically assigns one of these types to JavaScript values and will transparently allow&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Data_structures#dynamic_and_weak_typing\">reassignment and coercion<\/a>&nbsp;of these types. TypeScript\u2019s main objective is to change this aspect of JavaScript.<\/p>\n\n\n\n<p>In the code example below, I\u2019m using several JavaScript data types (i.e., Strings, Objects, and a Number):<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">let<\/span> firstName = <span class=\"hljs-string\">\"Sally\"<\/span>;\n<span class=\"hljs-keyword\">let<\/span> lastName = <span class=\"hljs-string\">\"Brown\"<\/span>;\n<span class=\"hljs-keyword\">let<\/span> age = <span class=\"hljs-number\">25<\/span>;\n\n<span class=\"hljs-keyword\">let<\/span> user1 = {\n  firstName: <span class=\"hljs-string\">\"Bill\"<\/span>,\n  lastName: <span class=\"hljs-string\">\"Fae\"<\/span>,\n  age: <span class=\"hljs-number\">45<\/span>,\n};\n\n<span class=\"hljs-keyword\">const<\/span> getFullName = <span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">user1<\/span><\/span>) =&gt;<\/span> {\n  <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">`<span class=\"hljs-subst\">${user.firstName}<\/span> <span class=\"hljs-subst\">${user.lastName}<\/span>`<\/span>;\n};\n\n<span class=\"hljs-built_in\">console<\/span>.log(getFullName(user)); <span class=\"hljs-comment\">\/\/ logs \"Bill Fae\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Without TypeScript, JavaScript would have no problem if we decided to change the value of age from a number to a string. Or if we decided to change the shape of the user object by adding a new property to the user Object called middleName. JavaScript would also be ok if we passed the getFullName() function an empty object, until the code is run.<\/p>\n\n\n\n<p>However, by using TypeScript and its type system, we can add type annotations to JavaScript that will make JavaScript act more like a programming language that does not allow type reassignment, changes, and JavaScript runtime errors.<\/p>\n\n\n\n<p>Below I am taking the previous code and adding all possible TypeScript type annotations to the JavaScript. You should carefully contrast this code to the previous code with no type annotations.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">let<\/span> firstName: <span class=\"hljs-built_in\">string<\/span> = <span class=\"hljs-string\">\"Sally\"<\/span>;\n<span class=\"hljs-keyword\">let<\/span> lastName: <span class=\"hljs-built_in\">string<\/span> = <span class=\"hljs-string\">\"Brown\"<\/span>;\n<span class=\"hljs-keyword\">let<\/span> age: <span class=\"hljs-built_in\">number<\/span> = <span class=\"hljs-number\">25<\/span>;\n\n<span class=\"hljs-comment\">\/\/ define the shape of a user object before defining a user object<\/span>\n<span class=\"hljs-keyword\">interface<\/span> UserTypeInterface {\n  firstName: <span class=\"hljs-built_in\">string<\/span>;\n  lastName: <span class=\"hljs-built_in\">string<\/span>;\n  age: <span class=\"hljs-built_in\">number<\/span>;\n}\n\n<span class=\"hljs-comment\">\/\/ define a user, annotate user with a type interface<\/span>\n<span class=\"hljs-keyword\">let<\/span> user1: UserTypeInterface = {\n  firstName: <span class=\"hljs-string\">\"Bill\"<\/span>,\n  lastName: <span class=\"hljs-string\">\"Fae\"<\/span>,\n  age: <span class=\"hljs-number\">45<\/span>,\n};\n\n<span class=\"hljs-comment\">\/\/ create a function, that only accepts values matching our UserTypeInterface,<\/span>\n<span class=\"hljs-comment\">\/\/ also, define what should be returned from this function<\/span>\n<span class=\"hljs-keyword\">const<\/span> getFullName = (user1: UserTypeInterface): <span class=\"hljs-function\"><span class=\"hljs-params\">string<\/span> =&gt;<\/span> {\n  <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">`<span class=\"hljs-subst\">${user.firstName}<\/span> <span class=\"hljs-subst\">${user.lastName}<\/span>`<\/span>;\n};\n\n<span class=\"hljs-built_in\">console<\/span>.log(getFullName(user)); <span class=\"hljs-comment\">\/\/ logs \"Bill Fae\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>By using TypeScript type annotations, we have layered a degree of control over our JavaScript, which enforces patterns from strongly typed languages. For&nbsp;<a href=\"https:\/\/www.typescriptlang.org\/play?#code\/DYUwLgBAZglgTgZzAOQIYFsQC4lxgOwHMIBeCAcgGVVhgBPcgbgFgAoUSYVJNTHMPEVIUAQnAD2Ad3xM2HCKkLZ8AV3QAjEHGEAmAKwtWbRSGHkdANnJs2BMFqioAxqYCqCLQBU6ABxABJfHs4RxcIAG82CGjoeB4MbFwCQiiYrni+JKJU6JMsVQ0tNgBfG3ZwCBUPOCx3L18AoIdnUzJI1hjYxBQErFEYWnIAGhyIdJ6+CgAxVBBh0byIABY9ErKqrQA6dBgAE13QXlaKSnsANznDNidxfCQIJTAplVoj4QAKDbgASn5BYhIAD4Iqk4OAVHB8BAAAYAEnCX02sG6R2KEHhiPGqOhhmKVyMBLKQA\">example,<\/a>&nbsp;I can no longer change the type of value that age can contain, or the shape of the user object, or create a function that will accept any type of value. What JavaScript would permit in our program is not permitted when using TypeScript. In the video below you will see that TypeScript reports issues where JavaScript does not.<\/p>\n\n\n\n\t\t<figure class=\"wp-block-jetpack-videopress jetpack-videopress-player\" style=\"\" >\n\t\t\t<div class=\"jetpack-videopress-player__wrapper\"> <iframe title=\"VideoPress Video Player\" aria-label='VideoPress Video Player' width='500' height='439' src='https:\/\/videopress.com\/embed\/D1XLtHSB?cover=1&amp;autoPlay=0&amp;controls=1&amp;loop=0&amp;muted=1&amp;persistVolume=0&amp;playsinline=0&amp;preloadContent=metadata&amp;useAverageColor=1&amp;hd=0' frameborder='0' allowfullscreen data-resize-to-parent=\"true\" allow='clipboard-write'><\/iframe><script src='https:\/\/v0.wordpress.com\/js\/next\/videopress-iframe.js?m=1725245713'><\/script><\/div>\n\t\t\t\n\t\t\t\n\t\t<\/figure>\n\t\t\n\n\n<p>The example above, where I explicitly provided all possible type annotations, is a bit contrived because explicitly providing all possible type annotations is verbose and ignores the fact that the type system can perform&nbsp;<a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/type-inference.html#handbook-content\">type inference<\/a>&nbsp;and infer certain types without explicit annotations.<\/p>\n\n\n\n\t\t<figure class=\"wp-block-jetpack-videopress jetpack-videopress-player\" style=\"\" >\n\t\t\t<div class=\"jetpack-videopress-player__wrapper\"> <iframe title=\"VideoPress Video Player\" aria-label='VideoPress Video Player' width='500' height='325' src='https:\/\/videopress.com\/embed\/1Om1ypXy?cover=1&amp;autoPlay=0&amp;controls=1&amp;loop=0&amp;muted=1&amp;persistVolume=0&amp;playsinline=0&amp;preloadContent=metadata&amp;useAverageColor=1&amp;hd=0' frameborder='0' allowfullscreen data-resize-to-parent=\"true\" allow='clipboard-write'><\/iframe><script src='https:\/\/v0.wordpress.com\/js\/next\/videopress-iframe.js?m=1725245713'><\/script><\/div>\n\t\t\t\n\t\t\t\n\t\t<\/figure>\n\t\t\n\n\n<p>The type system can infer a lot, but it does not infer everything needed to take full advantage of TypeScript. Most developers use inference where they can and then provide additional annotations where needed. In the code below, a mixture of inferences and annotations are used, which is likely closer to the reality of developing TypeScript in the wild.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">let<\/span> firstName = <span class=\"hljs-string\">\"Sally\"<\/span>; <span class=\"hljs-comment\">\/\/ simple values can be infered<\/span>\n<span class=\"hljs-keyword\">let<\/span> lastName = <span class=\"hljs-string\">\"Brown\"<\/span>; <span class=\"hljs-comment\">\/\/ simple values can be infered<\/span>\n<span class=\"hljs-keyword\">let<\/span> age = <span class=\"hljs-number\">25<\/span>; <span class=\"hljs-comment\">\/\/ simple values can be infered<\/span>\n\n<span class=\"hljs-comment\">\/\/ this is done so that the shape of this object is required<\/span>\n<span class=\"hljs-comment\">\/\/ the shape of an object can't be infered, we have to define it<\/span>\n<span class=\"hljs-keyword\">interface<\/span> UserTypeInterface {\n  firstName: <span class=\"hljs-built_in\">string<\/span>;\n  lastName: <span class=\"hljs-built_in\">string<\/span>;\n  age: <span class=\"hljs-built_in\">number<\/span>;\n}\n\n<span class=\"hljs-keyword\">let<\/span> user1: UserTypeInterface = {\n  firstName: <span class=\"hljs-string\">\"Bill\"<\/span>,\n  lastName: <span class=\"hljs-string\">\"Fae\"<\/span>,\n  age: <span class=\"hljs-number\">45<\/span>,\n};\n\n<span class=\"hljs-comment\">\/\/ explicitly tell this function the exact shape of the value passed in<\/span>\n<span class=\"hljs-comment\">\/\/ without this, the value passed into the fuction is given a type of any<\/span>\n<span class=\"hljs-comment\">\/\/ which depending upon how you configure TypeScript will throw an error<\/span>\n<span class=\"hljs-keyword\">const<\/span> getFullName = <span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">user1<\/span>: <span class=\"hljs-params\">UserTypeInterface<\/span><\/span>) =&gt;<\/span> {\n  <span class=\"hljs-comment\">\/\/ infer the return type<\/span>\n  <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">`<span class=\"hljs-subst\">${user.firstName}<\/span> <span class=\"hljs-subst\">${user.lastName}<\/span>`<\/span>;\n};\n\n<span class=\"hljs-built_in\">console<\/span>.log(getFullName(user)); <span class=\"hljs-comment\">\/\/ logs \"Bill Fae\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>We have only scratched the surface of the type system and type annotations in TypeScript. Imagine if you needed optional values. Or values that are of a limited specific value only. TypeScript can do all that and much more.<\/p>\n\n\n\n<p>As a small and simple example in the code below, I am telling TypeScript that the User object has an optional age property and a property called group with a fixed set of potential property values.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">interface<\/span> UserTypeInterface {\n  firstName: <span class=\"hljs-built_in\">string<\/span>;\n  lastName: <span class=\"hljs-built_in\">string<\/span>;\n  <span class=\"hljs-comment\">\/\/ used ?:, property is not required, but if it is provide has to be number<\/span>\n  age?: <span class=\"hljs-built_in\">number<\/span>;\n  group: <span class=\"hljs-string\">\"blueTeam\"<\/span> | <span class=\"hljs-string\">\"redTeam\"<\/span>; <span class=\"hljs-comment\">\/\/ has to be \"blueTeam\" or \"redTeam\"<\/span>\n}\n\n<span class=\"hljs-keyword\">let<\/span> user1: UserTypeInterface = {\n  firstName: <span class=\"hljs-string\">\"Bill\"<\/span>,\n  lastName: <span class=\"hljs-string\">\"Fae\"<\/span>, <span class=\"hljs-comment\">\/\/ age is optional<\/span>\n  group: <span class=\"hljs-string\">\"blueTeam\"<\/span>,\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The takeaway here should not be an in-depth understanding of the type system or annotations. I\u2019m only trying to communicate broadly the point of the type system and roughly what it fundamentally provides above and beyond JavaScript.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>TypeScript Reports JavaScript Syntax Errors as well as TypeScript Type Errors<\/strong><\/h3>\n\n\n\n<p>TypeScript not only reports issues found by the type system, but it can also report JavaScript syntax errors that will occur during runtime. By using TypeScript, these errors can be surfaced before runtime.<\/p>\n\n\n\n<p>The&nbsp;<a href=\"https:\/\/www.typescriptlang.org\/play?#code\/MYewdgzgLgBAagQQDIFUCiMC8MDkAzEEHAbgFgAoCxVDbHAI0JIooBsBTWAWwE8B5egCt2wWNgDeMCjBgAHAE4hZARgBceAIasI7KeRkKlAJlXKANHoOLZqsCCgARdngCWYdgBMKAXxaVyQA\">code<\/a>&nbsp;in the video below is filled with JavaScript syntax errors which TypeScript will report on.<\/p>\n\n\n\n\t\t<figure class=\"wp-block-jetpack-videopress jetpack-videopress-player\" style=\"\" >\n\t\t\t<div class=\"jetpack-videopress-player__wrapper\"> <iframe title=\"VideoPress Video Player\" aria-label='VideoPress Video Player' width='500' height='244' src='https:\/\/videopress.com\/embed\/kysedzRi?cover=1&amp;autoPlay=0&amp;controls=1&amp;loop=0&amp;muted=1&amp;persistVolume=0&amp;playsinline=0&amp;preloadContent=metadata&amp;useAverageColor=1&amp;hd=0' frameborder='0' allowfullscreen data-resize-to-parent=\"true\" allow='clipboard-write'><\/iframe><script src='https:\/\/v0.wordpress.com\/js\/next\/videopress-iframe.js?m=1725245713'><\/script><\/div>\n\t\t\t\n\t\t\t\n\t\t<\/figure>\n\t\t\n\n\n<h3 class=\"wp-block-heading\">TypeScript &amp; VS Code Provide a Superior Authoring Environment for JavaScript<\/h3>\n\n\n\n<p>Developers use TypeScript because it can convert the language into a language that mimics strongly typed languages by way of the type-system while also reporting JavaScript syntax errors without manually executing the JavaScript. However, it\u2019s not until these two aspects of TypeScript are combined with TypeScript development tools these merits become objective benefits.<\/p>\n\n\n\n<p>If a developer has the TypeScript compiler installed, then development environments like VS Code become objectively better in the following ways (click on the links and read the explanations in the VS code documentation):<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/code.visualstudio.com\/docs\/typescript\/typescript-editing\">Editing<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/code.visualstudio.com\/docs\/typescript\/typescript-refactoring\">Refactoring<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/code.visualstudio.com\/docs\/typescript\/typescript-debugging\">Debugging<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">TypeScript Can Compiling Current and Next Generation ECMAScript to Older ECMAScript<\/h3>\n\n\n\n<p>To review, TypeScript can:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Convert JavaScript into a language that resembles a strongly typed language.<\/li>\n\n\n\n<li>Preemptively notify you of JavaScript syntax errors while authoring.<\/li>\n\n\n\n<li>Integrate into VS Code and provide a superior development environment for authoring and maintaining code written in JavaScript.<\/li>\n<\/ol>\n\n\n\n<p>Lastly, it can take the most current ECMAScript specifications, as well as ECMAScript proposals (i.e., next-generation JavaScript), and compile it into older versions of JavaScript. This allows developers to make use of the most current and even futurist parts of JavaScript today but support much older runtimes that might not support current and futurist parts of JavaScript.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"before-learning-starts\">Before Learning Starts<\/h2>\n\n\n\n<p>We suggest becoming familiar with the following before you begin hands-on learning:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.typescriptlang.org\/play\">T<\/a><a href=\"https:\/\/www.typescriptlang.org\/play?target=1&amp;q=229#example\/hello-world\">y<\/a><a href=\"https:\/\/www.typescriptlang.org\/play?strict=false&amp;q=410#handbook-0\">peScript Playground<\/a>&nbsp;&#8211; A no-setup online simple TypeScript editor and compiler with tutorials.<\/li>\n\n\n\n<li><a href=\"https:\/\/code.visualstudio.com\/docs\/typescript\/typescript-tutorial\">TypeScript in VS Code<\/a>&nbsp;(or how TypeScript can augment your code editor of choice)<\/li>\n\n\n\n<li><a href=\"https:\/\/nodejs.org\/en\/\">Node.js<\/a>&nbsp;&amp;&nbsp;<a href=\"https:\/\/docs.npmjs.com\/cli\/v6\">npm<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/compiler-options.html\">TypeScript Compiler<\/a>&nbsp;(aka, tsc) &#8211; the Node.js CLI tool provided by the&nbsp;<a href=\"https:\/\/www.npmjs.com\/package\/typescript\">typescript package<\/a>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"start-learning\">Start Learning<\/h2>\n\n\n\n<p>Here is a three-step topical outline including learning resources to facilitate learning TypeScript:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><a href=\"#step-1-introductory-typescript\">Step 1 &#8211; Introductory TypeScript<\/a><\/strong><\/li>\n\n\n\n<li><strong><a href=\"#step-2-typescript-intermediate-to-advanced-concepts\">Step 2 &#8211; Intermediate to Advanced TypeScript<\/a><\/strong><\/li>\n\n\n\n<li><strong><a href=\"#step-3-typescript-friends\">Step 3 &#8211; TypeScript &amp; Friends<\/a><\/strong><\/li>\n<\/ul>\n\n\n\n<p>You must spend enough time in Step 1 before moving to Step 2. If you are intimidated or struggling with Step 2, return to Step 1 and spend more time with the fundamentals.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1 &#8211; Introductory TypeScript<\/strong><\/h3>\n\n\n\n<p>Video Courses:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/frontendmasters.com\/courses\/typescript-v4\/?utm_source=boost&amp;utm_medium=blog&amp;utm_campaign=boost\">TypeScript Fundamentals, v4<\/a>&nbsp;[$]<\/li>\n<\/ul>\n\n\n\n<p>Reading Material:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/typescript-from-scratch.html\">TypeScript for the New Programmer<\/a>&nbsp;[free]<\/li>\n\n\n\n<li><a href=\"https:\/\/code.visualstudio.com\/docs\/typescript\/typescript-compiling\">Compiling TypeScript<\/a>&nbsp;[free]<\/li>\n\n\n\n<li><a href=\"https:\/\/code.visualstudio.com\/docs\/typescript\/typescript-editing\">Editing TypeScript<\/a>&nbsp;[free]<\/li>\n\n\n\n<li><a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/typescript-in-5-minutes.html\">TypeScript for JavaScript Programmers<\/a>&nbsp;[free]<\/li>\n\n\n\n<li><a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/typescript-in-5-minutes-func.html\">TypeScript for Functional Programmers<\/a>&nbsp;[free]<\/li>\n\n\n\n<li><a href=\"https:\/\/www.typescript-training.com\/course\/fundamentals-v3\">TypeScript Fundamentals<\/a>, v3 [free]<\/li>\n\n\n\n<li><a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/intro.html\">The TypeScript Handbook<\/a>&nbsp;[free]<\/li>\n\n\n\n<li><a href=\"https:\/\/www.learningtypescript.com\/\">Learning TypeScript<\/a>&nbsp;[$]<\/li>\n<\/ul>\n\n\n\n<p>Exercises:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/typescript-tooling-in-5-minutes.html\">TypeScript Tooling in 5 minutes<\/a>&nbsp;[free]<\/li>\n\n\n\n<li><a href=\"https:\/\/www.codecademy.com\/learn\/learn-typescript\">Learn TypeScript<\/a>&nbsp;[free &#8211; account required]<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2 &#8211; TypeScript Intermediate to Advanced Concepts<\/strong><\/h3>\n\n\n\n<p>Video Courses:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/frontendmasters.com\/learn\/typescript\/?utm_source=boost&amp;utm_medium=blog&amp;utm_campaign=boost\">TypeScript Learning Path<\/a>\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/frontendmasters.com\/courses\/intermediate-typescript-v2\/?utm_source=boost&amp;utm_medium=blog&amp;utm_campaign=boost\">Intermediate TypeScript, v2<\/a>&nbsp;[$]<\/li>\n\n\n\n<li><a href=\"https:\/\/frontendmasters.com\/courses\/typescript-practice\/?utm_source=boost&amp;utm_medium=blog&amp;utm_campaign=boost\">Making TypeScript Stick<\/a>&nbsp;[$]<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>Reading Material:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/code.visualstudio.com\/docs\/typescript\/typescript-refactoring\">Refactoring TypeScript<\/a>&nbsp;[free]<\/li>\n\n\n\n<li><a href=\"https:\/\/code.visualstudio.com\/docs\/typescript\/typescript-debugging\">Debugging TypeScript<\/a>&nbsp;[free]<\/li>\n\n\n\n<li><a href=\"https:\/\/www.typescript-training.com\/course\/intermediate-v1\/01-project-setup\/\">Intermediate TypeScript<\/a>&nbsp;[free]<\/li>\n\n\n\n<li><a href=\"https:\/\/www.typescript-training.com\/course\/making-typescript-stick\">Making TypeScript Stick<\/a>&nbsp;[free]<\/li>\n\n\n\n<li><a href=\"https:\/\/www.typescriptlang.org\/play?strict=false&amp;q=410#handbook-0\">TypeScript Playground Handbook<\/a>&nbsp;[free]<\/li>\n<\/ul>\n\n\n\n<p>Exercises:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.typescriptlang.org\/play?strict=false&amp;q=410#handbook-2\">TypeScript Playground Examples<\/a>&nbsp;(click on \u201cExamples\u201d, read comments) [free]<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 3 &#8211; TypeScript &amp; Friends<\/strong><\/h3>\n\n\n\n<p>Video Courses:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.typescript-training.com\/course\/full-stack-typescript\">Full Stack TypeScript<\/a>&nbsp;&#8211; Combines TypeScript and GraphQL [$]<\/li>\n\n\n\n<li><a href=\"https:\/\/frontendmasters.com\/courses\/react-typescript-v2\/?utm_source=boost&amp;utm_medium=blog&amp;utm_campaign=boost\">React and TypeScript, v2<\/a>&nbsp;[$]<\/li>\n\n\n\n<li><a href=\"https:\/\/frontendmasters.com\/courses\/enterprise-typescript\/?utm_source=boost&amp;utm_medium=blog&amp;utm_campaign=boost\">Enterprise TypeScript<\/a>&nbsp;[$]<\/li>\n\n\n\n<li><a href=\"https:\/\/frontendmasters.com\/courses\/monorepos\/?utm_source=boost&amp;utm_medium=blog&amp;utm_campaign=boost\">JavaScript and TypeScript Monorepos<\/a>&nbsp;[$]<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"commonly-asked-questions\">Commonly Asked Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Should I learn JavaScript and TypeScript at the same time?<\/strong><\/h3>\n\n\n\n<p>If you are new to programming, JavaScript should be isolated and learned before learning TypeScript. TypeScript has a steep learning curve, steeper than JavaScript itself. Understanding both of these at the same time can be overwhelming. Before you approach TypeScript, learn JavaScript in-depth! Especially the fact that JavaScript is a&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Data_structures#dynamic_and_weak_typing\">dynamic and weakly typed language<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Is TypeScript itself a programming language?<\/strong><\/h3>\n\n\n\n<p>TypeScript is not a programming language itself. JavaScript is the programming language that TypeScript requires to be considered a programing language. When one suggests that TypeScript is a programming language, they are undoubtedly trying to communicate the belief that type annotations are a language. The type-annotation syntax intermingles with JavaScript syntax at development time. It is technically not a language without JavaScript. That is why \u201ca superset of JavaScript\u201d or \u201cJavaScript with types\u201d are the phrases used to describe TypeScript.<\/p>\n\n\n\n<p>This is JavaScript:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">let<\/span> count = <span class=\"hljs-number\">2<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This is JavaScript with a simple TypeScript type annotation (i.e., <code>: number<\/code>):<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">let<\/span> count: <span class=\"hljs-built_in\">number<\/span> = <span class=\"hljs-number\">2<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The programming language in question is still JavaScript, even though a TypeScript type annotation is being added to the JavaScript.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>What is the TypeScript Compiler?<\/strong><\/h3>\n\n\n\n<p>The TypeScript compiler&nbsp;<a href=\"https:\/\/github.com\/microsoft\/TypeScript\/#readme\"><\/a>(aka, tsc) is a Node.js CLI tool that can:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Compile\/transpile TypeScript to JavaScript.<\/li>\n\n\n\n<li>Statically analyze the validity of Typescript and JavaScript based on TypeScript rules, configurations, and type annotations or the lack thereof (can also be used by code editors to provide this information as you author JavaScript + TypeScript annotations).<\/li>\n<\/ol>\n\n\n\n<p>To gain a basic understanding of the compiler, I\u2019m going to walk us through a simplistic use of the tsc Node.js CLI tool.<\/p>\n\n\n\n<p>Go to&nbsp;<a href=\"https:\/\/stackblitz.com\/\">StackBlitz<\/a>&nbsp;and create a new Node.js project by clicking on \u201cNode.js Blank project\u201d at the top of the screen. You should be looking at a new Node.js project in your browser window.<\/p>\n\n\n\n<p>Go to the terminal of the StackBlitz project you just created and type the following in the terminal, and hit enter:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Bash\" data-shcb-language-slug=\"bash\"><span><code class=\"hljs language-bash\">npm i typescript<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Bash<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">bash<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This will install TypeScript and the tsc CLI tool into the StackBlitz Node.js project.<\/p>\n\n\n\n<p>You should be looking at something similar to this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"456\" src=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/typescript-stackblitz.png?resize=1024%2C456&#038;ssl=1\" alt=\"\" class=\"wp-image-3747\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/typescript-stackblitz.png?resize=1024%2C456&amp;ssl=1 1024w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/typescript-stackblitz.png?resize=300%2C134&amp;ssl=1 300w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/typescript-stackblitz.png?resize=768%2C342&amp;ssl=1 768w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/typescript-stackblitz.png?resize=1536%2C684&amp;ssl=1 1536w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/typescript-stackblitz.png?resize=2048%2C911&amp;ssl=1 2048w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p>With TypeScript now installed initialize a TypeScript project by creating a <code>.tsconfig<\/code> from running:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Bash\" data-shcb-language-slug=\"bash\"><span><code class=\"hljs language-bash\">tsc --init<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Bash<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">bash<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This will add a \u201ctsconfig.json\u201d file that is used to configure the TypeScript compiler. Open the \u201ctsconfig.json\u201d file and find and change \u201ctarget\u201d: \u201ces2016\u201d to \u201ctarget\u201d: \u201ces5\u201d. Save this file.<\/p>\n\n\n\n<p>Next, change the extension of the \u201cindex.js\u201d file to \u201cindex.ts\u201d. And replace the contents of the file with:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">let<\/span> count: <span class=\"hljs-built_in\">number<\/span> = <span class=\"hljs-number\">2<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Save the file and now let\u2019s use the TypeScript compiler (i.e., tsc) to type check and compile\/transpile the \u201cindex.ts\u201d file to JavaScript.<\/p>\n\n\n\n<p>In the terminal run:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Bash\" data-shcb-language-slug=\"bash\"><span><code class=\"hljs language-bash\">tsc index.ts<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Bash<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">bash<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This will create a new file called \u201cindex.js\u201d. Open the \u201cindex.js\u201d file and take note that the compiler removed the TypeScript type annotation \u201c: number\u201d and change \u201clet\u201d to \u201cvar\u201d (Note that \u201clet\u201d was not yet supported in ES5).<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"489\" src=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/typescript-simple.png?resize=1024%2C489&#038;ssl=1\" alt=\"\" class=\"wp-image-3746\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/typescript-simple.png?resize=1024%2C489&amp;ssl=1 1024w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/typescript-simple.png?resize=300%2C143&amp;ssl=1 300w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/typescript-simple.png?resize=768%2C367&amp;ssl=1 768w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/typescript-simple.png?resize=1536%2C733&amp;ssl=1 1536w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/typescript-simple.png?resize=2048%2C978&amp;ssl=1 2048w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p>Next, let\u2019s purposely create a TypeScript error so we can see the compiler report a type issue.<\/p>\n\n\n\n<p>Replace the contents of the \u201cindex.ts\u201d file with:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">let<\/span> count: <span class=\"hljs-built_in\">string<\/span> = <span class=\"hljs-number\">2<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Save this file.<\/p>\n\n\n\n<p>TypeScript will certainly have an issue with this because the data type of 2 is not a string but a number.<\/p>\n\n\n\n<p>Now run the compiler again using:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Bash\" data-shcb-language-slug=\"bash\"><span><code class=\"hljs language-bash\">tsc index.ts<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Bash<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">bash<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that both the code editor (red squiggly line under \u201ccount\u201d) and the \u201ctsc\u201d CLI tool are reporting a TypeScript error.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"566\" src=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/typescript-error-1024x566.png?resize=1024%2C566&#038;ssl=1\" alt=\"\" class=\"wp-image-3745\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/typescript-error.png?resize=1024%2C566&amp;ssl=1 1024w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/typescript-error.png?resize=300%2C166&amp;ssl=1 300w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/typescript-error.png?resize=768%2C424&amp;ssl=1 768w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/typescript-error.png?resize=1536%2C848&amp;ssl=1 1536w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/typescript-error.png?resize=2048%2C1131&amp;ssl=1 2048w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p>In this short exercise, we used the TypeScript compiler to compile\/transpile TypeScript to JavaScript and took note of the type-checking capabilities of the compiler as well as the editor.<\/p>\n\n\n\n<p><strong>What is Type Checking?<\/strong><\/p>\n\n\n\n<p>Type checking is the process of analyzing TypeScript syntax for TypeScript soundness based on TypeScript configurations as well as TypeScript type annotations or lack thereof.<\/p>\n\n\n\n<p>This process can occur when compiling\/transpiling using the \u201ctsc\u201d CLI tool or when authoring TypeScript code. For example, code editors like VSCode (via the language service) can use the TypeScript compiler in the background to perform type-checking in real-time:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"309\" src=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/incorrect-type-error-1024x309.png?resize=1024%2C309&#038;ssl=1\" alt=\"\" class=\"wp-image-3744\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/incorrect-type-error.png?resize=1024%2C309&amp;ssl=1 1024w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/incorrect-type-error.png?resize=300%2C90&amp;ssl=1 300w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/incorrect-type-error.png?resize=768%2C232&amp;ssl=1 768w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/incorrect-type-error.png?w=1078&amp;ssl=1 1078w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p>Most developers use their code editor to do type checking in real-time and then use build tools to transpile \u201c.ts\u201d and \u201c.tsx\u201d files to \u201c.js\u201d files. For example<a href=\"https:\/\/vitejs.dev\/guide\/features.html#typescript\">, out of the box, Vite will compile both \u201c.ts<\/a>\u201d&nbsp;<a href=\"https:\/\/vitejs.dev\/guide\/features.html#typescript\">and \u201c.tsx<\/a>\u201d&nbsp;<a href=\"https:\/\/vitejs.dev\/guide\/features.html#typescript\">files to JavaScript files<\/a>&nbsp;but not perform type checking. It leaves that up to the code editor.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"whats-next\">What\u2019s Next?<\/h2>\n\n\n\n<p>After reading this guide and consuming the learning material a possible next step would be to investigate the following tools that often go hand and hand with TypeScript:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/prettier.io\/\">Prettier<\/a>&nbsp;&#8211; An opinioned code formatter.<\/li>\n\n\n\n<li><a href=\"https:\/\/typescript-eslint.io\/\">ESLint &amp; ESLint TypeScript<\/a>&nbsp;&#8211; a set of ESLint rules built to support TypeScript and Prettier (i.e., an ESLint TypeScript parser &amp; plugin).<\/li>\n<\/ul>\n\n\n\n<p>Make sure to check out the&nbsp;<a href=\"https:\/\/frontendmasters.com\/learn\/typescript\/?utm_source=boost&amp;utm_medium=blog&amp;utm_campaign=boost\">learning path on TypeScript<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;TypeScript has a steep learning curve, steeper than JavaScript itself. Understanding both of these at the same time can be overwhelming.&#8221;<\/p>\n","protected":false},"author":31,"featured_media":3766,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3728","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-post"],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/ts-logo-512.png?fit=512%2C512&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/3728","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/comments?post=3728"}],"version-history":[{"count":13,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/3728\/revisions"}],"predecessor-version":[{"id":3768,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/3728\/revisions\/3768"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media\/3766"}],"wp:attachment":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media?parent=3728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/categories?post=3728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/tags?post=3728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}