{"id":18625,"date":"2022-12-21T10:26:26","date_gmt":"2022-12-21T09:26:26","guid":{"rendered":"https:\/\/www.oimmei.com\/typescript-5-trucchi-sviluppo\/"},"modified":"2025-06-03T15:13:40","modified_gmt":"2025-06-03T13:13:40","slug":"typescript-5-tricks-development","status":"publish","type":"post","link":"https:\/\/www.oimmei.com\/en\/typescript-5-tricks-development\/","title":{"rendered":"5 useful TypeScript tips &amp; tricks"},"content":{"rendered":"\r\n<p class=\"wp-block-paragraph\">We planned to release this article a long time ago, but we had some issues compiling it. We finally solved every type error, so here we go.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">TypeScript<\/h2>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Oh, TypeScript. The open source programming language developed by Microsoft as a superset of JavaScript doesn\u2019t need any introduction at this point. With tools like <a href=\"https:\/\/reactjs.org\/\">React<\/a>, <a href=\"https:\/\/angular.io\/\">Angular<\/a> and <a href=\"https:\/\/nodejs.org\/en\/\">Node.js<\/a> being more and more popular every day, the need to develop robust and easy-to-maintain JavaScript applications is impossible to ignore, and the static typing provided by TypeScript helps a lot in that regard.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">If you\u2019re part of the <a href=\"https:\/\/www.theregister.com\/2022\/06\/22\/stackoverflow_survey\/\">increasingly small<\/a> group of developers who are yet to adopt TypeScript in their daily work, don\u2019t worry: if you know JavaScript, its statically typed cousin is not difficult to approach. Being a superset, TypeScript can natively compile any JavaScript code &#8211; with the exception of some type error -, and allows you to start gradually using its features.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The purpose of this article is not to introduce TypeScript, which would probably be redundant for many of you. Instead, I want to show off some interesting features I learned while working with <a href=\"https:\/\/en.wikipedia.org\/wiki\/Anders_Hejlsberg\">Anders Hejlsberg<\/a>\u2019s language. TypeScript offers a lot of tools besides the most obvious ones, and they may surprise even developers with a fair share of experience with it. They can be useful to make our projects even more robust and readable or to save some unnecessary code.So, here\u2019s 5 tips and tricks you can use with TypeScript, starting with the most popular and well-known and then going down to the most obscure and situational ones, all tested on the current version of the language (4.8) and with full examples on the <a href=\"https:\/\/www.typescriptlang.org\/play\">official playground<\/a>.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\">Utility type: Omit e Pick<\/h3>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Utility types are utilities &#8211; duh &#8211; globally available in every TypeScript application. They are tools that, given a type A, like an interface, can create a type B as a somewhat modified version of A.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">There are a lot of utility types and they\u2019re well documented in the <a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/utility-types.html\">official documentation<\/a>, so in this article I\u2019ll just show off two of the ones I\u2019ve ended up using the most in our own projects. <a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/utility-types.html#omittype-keys\">Omit<\/a> is a utility type that, given a type A with a bunch of properties, creates a type B with all the properties from A except the ones provided in its definition.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-syntaxhighlighter-code highlighted-error\">\/\/ Starting type:\r\ninterface TypeA {\r\n  id: string\r\n\r\n  name: string\r\n\r\n  description: string\r\n\r\n  price: number\r\n}\r\n\r\n\/\/ New type created from the starting one, excluding the properties description and price:\r\ntype TypeB = Omit&lt;TypeA, \"description\" | \"price\"&gt;;\r\n\r\n\/\/ Creating a TypeB object:\r\nconst objB: TypeB = {\r\n  id: \"1001\", \/\/ OK.\r\n\r\n  name: \"Name\", \/\/ OK.\r\n\r\n  description: \"Description\", \/\/ Invalid: this property doesn't exist in TypeB.\r\n}\r\n\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong><a href=\"https:\/\/www.typescriptlang.org\/play?#code\/PTAEGUBcEMCdIJYDsDmpIE8AOBTAXALABQykOsAZtAMY6gAq2OAgqAN7GigIAmeoAZ0ixkKYp1BJoAW3yDho8US48cA6iKyIA9kn5CRqJVywja-JAFdpAI3LEAvkpCgAcjgDu6JqA05oZDygFLDa0ugAFnRCcIiooLo4ADSgOAAe1AA2ljyikXSm2rjwCGqgquqaOkig0EhBpgjmxJi4DEwAQqAAvKAA8tIIkAA8jLjMKQBEFRoIWgi6k6AAPqCTjbSTAHwA3M5gAMKw-nFo0O24Xdo2AFY41JCERNS6Qgm3HfxjOF29HMrcPhrACMAAZQcDJikXH0ANIAOmMkhkckmrhRUNAMIRSJmVQWejWABE1LN5otoWAAJJIABu0EyvH4kAiCAEoEKxUw5W0aiQAHJIKk0myhcgLj9EUQnEQgA\" target=\"_blank\" rel=\"noreferrer noopener\">Try it out on playground<\/a><\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">This utility can be really useful if you need to create partial objects of a specific type while also taking advantage of the constraints provided by the type itself, like the mandatory properties. Thus, Omit can be more robust and specific than <a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/utility-types.html#partialtype\">Partial<\/a>, which just makes every property of a type optional, although it\u2019s a little harder to use.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Omit is also helpful for code reuse. Picture this: somewhere in your application, potentially in a library, there is a <strong>TextInputProps<\/strong> type, which is an interface for the property of a text input field. You want to create a new input type, which works the same as the other one but only handles numbers instead of strings. You&#8217;d like to be able to just extend <strong>TextInputProps<\/strong> to change the type for the property <strong>value<\/strong>, but\u2026<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-syntaxhighlighter-code highlighted-error\">interface TextInputProps {\r\n  label: string\r\n\r\n  name: string\r\n\r\n  value: string\r\n}\r\n\r\ninterface NumberInputProps extends TextInputProps {\r\n  value: number\r\n}\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.typescriptlang.org\/play?ssl=12&amp;ssc=1&amp;pln=1&amp;pc=1#code\/JYOwLgpgTgZghgYwgAgCoQB5gJIgA4CuYAClAPZ4DOyA3gLABQyyANnAEYQsBcylYUUAHNGjZiDgBbCL36CQIhmOQA3OCwIy+A4YwC+ohqEixEKAHIFJnKLkIlyVZJkggAJtXRY7RUhWr0TKrqmrwgVjb6jEA\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Try it out on playground<\/strong><\/a><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Oh no! You can\u2019t extend the interface because the types aren\u2019t compatible! What now? Do you have to replicate the whole interface, or create a new one to encapsulate the common fields?Not necessarily: Omit comes to your aid.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Instead of extending <strong>TextInputProps<\/strong>, you can extend a modified version which includes every property except for <strong>value<\/strong>. This allows you to redefine the property without any issue, even if you\u2019re not meeting the constraint of the starting type. Keep in mind the new interface won\u2019t be an actual extension of the starting one anymore in terms of object-oriented programming &#8211; in other words: <strong>NumberInputProps<\/strong> objects won\u2019t be <strong>TextInputProps<\/strong> objects -, but it\u2019s not a problem in this case.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-syntaxhighlighter-code\">interface TextInputProps {\r\n  label: string\r\n\r\n  name: string\r\n\r\n  value: string\r\n}\r\n\r\ninterface NumberInputProps extends Omit&lt;TextInputProps, \"value\"&gt; {\r\n  value: number\r\n}\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.typescriptlang.org\/play?ssl=12&amp;ssc=1&amp;pln=1&amp;pc=1#code\/JYOwLgpgTgZghgYwgAgCoQB5gJIgA4CuYAClAPZ4DOyA3gLABQyyANnAEYQsBcylYUUAHNGjZiDgBbCL36CQIhmOQA3OCwIy+A4YwC+ohqEixEKAHIFJnKLkIlyVZJkggAJtQDyk4GAA86Fh2RKQUlAA0yABEahoQUQB8tMqxmrwgVjb6jEA\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Try it out on playground<\/strong><\/a><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">One less thing to do before you can go home and turn on your PlayStation! Woo-ooh!*<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">As I\u2019m sure you\u2019ll have guessed, <a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/utility-types.html#picktype-keys\">Pick<\/a> works in the opposite way, meaning it creates a type B with only the given subset of type A, and it\u2019s just as useful.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-syntaxhighlighter-code highlighted-error\">\/\/ Starting type:\r\ninterface TypeA {\r\n  id: string\r\n\r\n  name: string\r\n\r\n  description: string\r\n\r\n  price: number\r\n}\r\n\r\n\/\/ New type created from the starting one, with just the id and name properties:\r\ntype TypeB = Pick&lt;TypeA, \"id\" | \"name\"&gt;;\r\n\r\n\/\/ Creating a TypeB object:\r\nconst objB: TypeB = {\r\n  id: \"1001\", \/\/ OK.\r\n\r\n  name: \"Name\", \/\/ OK.\r\n\r\n  description: \"Description\", \/\/ Invalid: this property doesn't exist in TypeB.\r\n}\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.typescriptlang.org\/play?#code\/PTAEGUBcEMCdIJYDsDmpIE8AOBTAXALABQykOsAZtAMY6gAq2OAgqAN7GigIAmeoAZ0ixkKYp1BJoAW3yDho8US48cA6iKyIA9kn5CRqJVywja-JAFdpAI3LEAvkpCgAcjgDu6JqA05oZDygFLDa0ugAFnRCcIiooLo4ADSgHgiQEaAAVpZCkXS8oNBIQVKyoKbauPAIaoREmLgMTABCoAC8oAAKCNQA1gA8jLjMKQBEvGOgAD6gY2U4YwB8ANzOYADCsP5xaNDNuG3aNlk41JD11Lp5x1kt-MM4bZ0cytx8cwCMAAzfn2MpFwAeQA0gA6YySGRyMauaEA0DA8GQ1TqTQ6PRzAAiag0CC0CF0CJcAEkkAA3aAAG14-AyCAEFVC1UwoB42jUSAA5JBQDgAB4M3nIA5PCFEJxEIA\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Try it out on playground<\/strong><\/a><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">(*Note for all the mr. and mrs. Know-it-all who were already typing their comments: yeah, I know, this problem would be way better solved with <a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/2\/generics.html\">generics type<\/a>. It was just an example to explain the feature, ok? Keep your hands off the keyboard and I won\u2019t humiliate you in Elden Ring PVP tonight.)<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\">Using the type of a property<\/h3>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">This one is really simple, but still useful in order to avoid code replication.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Here\u2019s a typical situation in a relatively complex TypeScript project: you have an interface, and this interface has a bunch of properties, each with its own type. You have to create a new object implementing this interface, but one of the values is difficult to calculate, and you want to use a different variable before setting it in the object itself. The question is: how can you give the correct type to said variable?<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The easiest answer is to just repeat the type of the property when you declare the variable.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-syntaxhighlighter-code\">\/\/ Interface:\r\ninterface SomeInterface {\r\n  id: number\r\n\r\n  code: string\r\n\r\n  name: string\r\n}\r\n\r\n\/\/ Declaring a variable in order to calculate the value for code:\r\nlet code: string; \/\/ &lt;- Repeating the type\r\n\r\n\/\/ An incredibly and overwhelmingly complex initialization logic:\r\ncode = \"CODE\";\r\n\r\nconst someObject: SomeInterface = {\r\n  id: 1,\r\n  code: code,\r\n  name: \"Name\",\r\n}<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong><a href=\"https:\/\/www.typescriptlang.org\/play?ssl=20&amp;ssc=2&amp;pln=1&amp;pc=1#code\/PTAEEkDsBcFMCcBmBDAxrAXAWAFAEsYEV1QBlAewFtYo4k1ZQBvXUUPAEw1EgFdKARglytQqch0ygAztHgEA5iJxtIyat1nzISnAF9lIUABFYqADbJtC0MlAA3K3mQDzjAqHLxJ8UNHJiyOaovJZwfgAWjI7mvIyIXmISmLhu0EmSmnKKANygRgA8ALSgAEqwAA6wyNCKkYzQAJ5VhmAAgpDskKjwsBx4ro22kBye9ggA7lHmlIrmQ+KUFW4AHl14tUF4AF41eOSd5uQKeKjYOOKSoAC8oABEAMIA8sYAonc5yuKQsjJUsE8BAArMzQbgUai0IgMG7MUScbgARgANKJLlJ0aiVDx1FI7gA5XF3LF6IA\" target=\"_blank\" rel=\"noreferrer noopener\">Try it out on playground<\/a><\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">However, this repetition is actually unnecessary. When you have a type with some properties, such as an interface, TypeScript allows you to reference the type of a specific property by using the square bracket notation, just like you were accessing a common JavaScript object.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-syntaxhighlighter-code\">\/\/ Interface:\r\ninterface SomeInterface {\r\n  id: number\r\n\r\n  code: string\r\n\r\n  name: string\r\n}\r\n\r\n\/\/ Declaring a variable in order to calculate the value for code:\r\nlet code: SomeInterface[\"code\"]; \/\/ &lt;- No repetition!\r\n\r\n\/\/ An incredibly and overwhelmingly complex initialization logic:\r\ncode = \"CODE\";\r\n\r\nconst someObject: SomeInterface = {\r\n  id: 1,\r\n  code: code,\r\n  name: \"Name\",\r\n}<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong><a href=\"https:\/\/www.typescriptlang.org\/play?ssl=20&amp;ssc=2&amp;pln=1&amp;pc=1#code\/PTAEEkDsBcFMCcBmBDAxrAXAWAFAEsYEV1QBlAewFtYo4k1ZQBvXUUPAEw1EgFdKARglytQqch0ygAztHgEA5iJxtIyat1nzISnAF9lIUABFYqADbJtC0MlAA3K3mQDzjAqHLxJ8UNHJiyOaovJZwfgAWjI7mvIyIXmISmLhu0EmS3BTUtEQMANoAROKShQC6ANygRgA8ALSgAHIB8LAADrDQeF3kkACEhmAAgpDskKitHHiuAJ62kBye9ggA7lHmlIrmc+KUbW4AHmPdzuZ4AF7IPaPm5Ap4qNg4JYwAvKCFAMIA8sYAooUKspxJBZDIqLBvgIAFZmaBZCG5egkd4sFTsLigACMABpRC9uC88ei1BoPo11LBCsS9EA\" target=\"_blank\" rel=\"noreferrer noopener\">Try it out on playground<\/a><\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Only square brackets, though. Don\u2019t try it with the dot notation.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">With this solution, the interface will be the only one keeping the information about the type of the property, and if said type ever changed in the future you\u2019d have one less line of code to worry about. Let\u2019s be clear: if the property switches to a new type that\u2019s incompatible with the old one you\u2019ll still have to fix the initialization, but you won\u2019t have to change the static type for the declaration of the variable.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Likewise, you can access the typing of the element in an array type by using the type of its index, meaning <em>number<\/em>.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-syntaxhighlighter-code\">\/\/ Interface:\r\ninterface SomeInterface {\r\n  id: number\r\n\r\n  code: string\r\n\r\n  name: string\r\n}\r\n\r\n\/\/ Declaring an array of SomeInterface objects:\r\ntype SomeInterfaceArray = SomeInterface[];\r\n\r\n\/\/ Using the array type to get the element type:\r\nconst someObject: SomeInterfaceArray[number] = {\r\n  id: 1,\r\n  code: \"CODE\",\r\n  name: \"Name\",\r\n}<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.typescriptlang.org\/play?#code\/PTAEEkDsBcFMCcBmBDAxrAXAWAFAEsYEV1QBlAewFtYo4k1ZQBvXUUPAEw1EgFdKARglytQqch0ygAztHgEA5iJxtIyat1nzISnAF9lIUABFYqADbJtC0Mki348ZAE9Q5RGSo1C9EuQEAVmbQ0tg40M4ADowU1LREDACCji6gALyecT7EsADaALoA3IZgAKrSiqDQABaMVk6uEdFV5KAKsNBVtaCw5rDUMFVRmLjikLIyXgDygcHcsd50OckNuXyCCPnpzKKc3ACMADSi4pLcAEQAwlPGAKLnxyo86lLnAHIvD7h6QA\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Try it out on <\/strong><\/a><strong><a href=\"https:\/\/www.typescriptlang.org\/play?ssl=18&amp;ssc=2&amp;pln=1&amp;pc=1#code\/PTAEEkDsBcFMCcBmBDAxrAXAWAFAEsYEV1QBlAewFtYo4k1ZQBvXUUPAEw1EgFdKARglytQqch0ygAztHgEA5iJxtIyat1nzISnAF9lIUABFYqADbJtC0Mki348ZAE9Q5RGSo1C9EuQEAVmbQ0tg40M4ADowU1LREDACCji6gALyecT7EsADaALoA3IZgAKrSiqDQABaMVk6uEdFV5KAKsNBVtaCw5rDUMFVRmLjikLIyXgDygcHcsd50OckNuXyCCPnpzKKc3ACMADSi4pLcAEQAwlPGAKLnxyo86lLnAHIvD7h6QA\" target=\"_blank\" rel=\"noreferrer noopener\">playground<\/a><\/strong><\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\">Type narrowing: discriminated union<\/h3>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">This is one of my personal favorites, and a very interesting one for those who are used to traditional object-oriented programming languages.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">First of all: I don\u2019t think <a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/2\/everyday-types.html#union-types\">union types<\/a> are a mystery to any TypeScript developer. It\u2019s the feature that allow you to create a new type by combining existing types with the pipe operator (|).<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-syntaxhighlighter-code highlighted-error\">\/\/ Union type:\r\ntype SomeUnion = string | boolean;\r\n\r\n\/\/ Valid:\r\nconst someVariable1: SomeUnion = \"String\";\r\n\r\n\/\/ Valid:\r\nconst someVariable2: SomeUnion = false;\r\n\r\n\/\/ Not valid:\r\nconst someVariable3: SomeUnion = 10;<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong><a href=\"https:\/\/www.typescriptlang.org\/play?#code\/PTAEFUDsEsHtNAFwJ4AcCmAuAsAKBRqAMqwC26UcCAvKAM6IBO0kA5qAD6gBGssANugCGkANx48IUADUh-aABMcuAMbwG9MulnMh3QQEZMxLZXihaAIiJMWrS+NySws+UrxrIGulp3Q9ggBMxiTkZjSgAGZydOiOzqAAcrCIoABucorKnt6+Qrr66ADMIaYw5rQGAAyiQA\" target=\"_blank\" rel=\"noreferrer noopener\">Try it out on playground<\/a><\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Creating a union type using values instead of types is also a well-known feature, since scalar values are considered valid types by TypeScript. This means you can type a variable or a property to have one of a specific list of values, for example one of three strings.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-syntaxhighlighter-code highlighted-error\">\/\/ Union type:\r\ntype SomeUnion = \"string_1\" | \"string_2\" | \"string_3\";\r\n\r\n\/\/ Valid:\r\nconst someVariable1: SomeUnion = \"string_1\";\r\n\r\n\/\/ Not valid:\r\nconst someVariable2: SomeUnion = \"string_5\";<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.typescriptlang.org\/play?ssl=8&amp;ssc=45&amp;pln=1&amp;pc=1#code\/PTAEFUDsEsHtNAFwJ4AcCmAuAsAKBRqAMqwC26UcCAvKAEQDOiATtJAOYD6AjHaAD70mrDpwBMfQYxZsuAZjoBuPHhCgAagEMANtAAmOXAGN4TUAzLotrTQCNt6bpmKXK8ULWkiuvZblVgAHKwiKAAbjr6hiaQZhbk1tB2DmLOJORuNEIyogCsSkA\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Try it out on playground<\/strong><\/a><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">It\u2019s all pretty boring so far, but give me a moment: here comes the fun part. Did you know you can use union types in order to discriminate between different types and take advantage of static typing for them? That\u2019s a little convoluted, I know. Let\u2019s make another example.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Let\u2019s say your project calls or exposes a REST API. The API response structure includes:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>a <strong>status<\/strong>, <em>SUCCESS<\/em> or <em>FAIL<\/em>;<\/li>\r\n\r\n\r\n\r\n<li>the body <strong>data<\/strong>, whose type doesn\u2019t matter here;<\/li>\r\n\r\n\r\n\r\n<li>an <strong>error_message<\/strong> string, which gives the caller some description of any error occurred. The response will contain <strong>error_message<\/strong> only when its <strong>status<\/strong> is <em>FAIL<\/em>.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">How could you model this structure into your project?Thinking about the typical object-oriented style, you could end up with an interface like the one below, with <strong>status<\/strong> that can assume either one of the two values and the optional field <strong>error_message<\/strong>.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-syntaxhighlighter-code\">interface ApiResponse {\r\n  status: \"SUCCESS\" | \"FAILURE\"\r\n\r\n  data: any\r\n\r\n  \/\/ Will only be valued on FAILURE.\r\n  error_message?: string\r\n}<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.typescriptlang.org\/play?#code\/JYOwLgpgTgZghgYwgAgIIAdgCUIGd0D2IuKA3gLABQyyuYcYArrgFzIBEAygKoDCvAUU6d2yAD4cAYqgCSAGW5YB7KlRoATBnDZwQAT1XVkAemPIA6sAA2V5ESt7kAIxQA3OFcYR1dkMmnyigIAdGrI0FAEUAD6ALZ4uHAA5hAA-Gx0UKBJVAC+QA\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Try it out on playground<\/strong><\/a><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The disadvantage here is that you have no static validation for <strong>error_message<\/strong>. You have to make sure the field is only valued when it should, and if you do something wrong you won\u2019t realize it before debugging.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-syntaxhighlighter-code\">interface ApiResponse {\r\n  status: \"SUCCESS\" | \"FAILURE\"\r\n\r\n  data: any\r\n\r\n  \/\/ Will only be valued on FAILURE.\r\n  error_message?: string\r\n}\r\n\r\n\/\/ Valid: the compiler is unable to tell this is wrong.\r\nconst response: ApiResponse = {\r\n  status: \"SUCCESS\",\r\n  data: {},\r\n  error_message: \"An error message\"\r\n}<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.typescriptlang.org\/play?#code\/JYOwLgpgTgZghgYwgAgIIAdgCUIGd0D2IuKA3gLABQyyuYcYArrgFzIBEAygKoDCvAUU6d2yAD4cAYqgCSAGW5YB7KlRoATBnDZwQAT1XVkAemPIAatCgAD5IUIAbLbQIOCyUMgRxc79cFp6JlxkaXlFAQA6NWQrAigAfQBbPFw4AHMIAH42OihQdKoAX0NTCzgHYHUCNmAHLwIkzCcweJQQIjtGACfkdQh1RigoFAQACxQAR0Y8elpgJjgAL2AiFAAL2OGtaMoEIjpkEfwDiDYMbDxCYhQAXmQKIzoGZjYuPkFhdgAaGM16NikIq-IxxRIpXBpTJvACyqQy6VWfQCYIgKkoRSAA\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Try it out on playground<\/strong><\/a><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Luckily, union types unlock a powerful alternative to solve this problem: <a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/2\/narrowing.html#discriminated-unions\">discriminated unions<\/a>, which are part of the broader <a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/2\/narrowing.html\">type narrowing<\/a> topic. A lot could be written on type narrowing, but let\u2019s just say it\u2019s the group of features that allows TypeScript to figure out the type of a certain variable given your type handling and the flow of your application. Let\u2019s use discriminated unions to refactor the previous example like this.<\/p>\r\n\r\n\r\n\r\n<ol class=\"wp-block-list\">\r\n<li>Instead of one interface, let\u2019s create two: one for a successful response and one for a failure response.<\/li>\r\n\r\n\r\n\r\n<li>For <strong>ApiSuccessResponse<\/strong>, let\u2019s type <strong>status<\/strong> so it can only have <em>SUCCESS<\/em> as a value, and let\u2019s not include <strong>error_message<\/strong> at all. For <strong>ApiFailureResponse<\/strong>, <strong>status<\/strong> will only have <em>FAIL<\/em> as a value and <strong>error_message<\/strong> will be a mandatory string.<\/li>\r\n\r\n\r\n\r\n<li>Finally, let\u2019s create a third interface <strong>ApiResponse<\/strong> as a union of the previous two.<\/li>\r\n<\/ol>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Here it is.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-syntaxhighlighter-code\">interface ApiSuccessResponse {\r\n  status: \"SUCCESS\"\r\n\r\n  data: any\r\n}\r\n\r\ninterface ApiFailureResponse {\r\n  status: \"FAIL\"\r\n\r\n  data: any\r\n\r\n  error_message: string\r\n}\r\n\r\ntype ApiResponse = ApiSuccessResponse | ApiFailureResponse;\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.typescriptlang.org\/play?#code\/JYOwLgpgTgZghgYwgAgIIAdgGUCuCkDOBAShAegPYgEoDeAsAFDLIFhxg4EBcyARFgCqAYWEBRLFj5MmLACYc4vOCACeTAL4zGoSLEQoMwAGJxgAGxxQIpclRrIGzVu049+x1AEkAMtMayyArsymraLNBQFFAA+gC2ZARwAOYQvGxQoMma2mCq6IaYtpTUKAC8aJi4+InF9igAPpUmZpbWdaUA3ExAA\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Try it out on playground<\/strong><\/a><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">With this typing, the compiler can discriminate between the different types of response, and will be able to statically tell you if <strong>error_message<\/strong> should be there, for both write and read access.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-syntaxhighlighter-code highlighted-error\">interface ApiSuccessResponse {\r\n  status: \"SUCCESS\"\r\n\r\n  data: any\r\n}\r\n\r\ninterface ApiFailureResponse {\r\n  status: \"FAIL\"\r\n\r\n  data: any\r\n\r\n  error_message: string\r\n}\r\n\r\ntype ApiResponse = ApiSuccessResponse | ApiFailureResponse;\r\n\r\n\/\/ Not valid: a SUCCESS response can't have an error_message.\r\nconst response1: ApiResponse = {\r\n  status: \"SUCCESS\",\r\n  data: {},\r\n  error_message: \"An error message\",\r\n}\r\n\r\n\/\/ Not valid: a FAIL response must have an error_message.\r\nconst response2: ApiResponse = {\r\n  status: \"FAIL\",\r\n  data: {},\r\n}\r\n\r\n\/\/ Valid:\r\nconst response3: ApiResponse = {\r\n  status: \"SUCCESS\",\r\n  data: {},\r\n}\r\nconst response4: ApiResponse = {\r\n  status: \"FAIL\",\r\n  data: {},\r\n  error_message: \"An error message\",\r\n}<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.typescriptlang.org\/play?#code\/JYOwLgpgTgZghgYwgAgIIAdgGUCuCkDOBAShAegPYgEoDeAsAFDLIFhxg4EBcyARFgCqAYWEBRLFj5MmLACYc4vOCACeTAL4zGoSLEQoMwAGJxgAGxxQIpclRrIGzVu049+x1AEkAMtMayyArsymraLNBQFFAA+gC2ZARwAOYQvGxQoMma2mCq6IaYtpTUKAC8aJi4+InF9igAPpUmZpbWdaUA3NoA9D3IAHIUYMgAbnDmwHLKyEKiEljI1nalyAgqAOQjABZwoygqyJHR8YkpEAB0TAj2I8slNACMvEYdDhVOLGwcXLwCIuJJHwADSBYJKRwaUHOY6xBJEc5-VAgI5QKJQZDwpKpEE5AKMPqDYZjCZTGaeXxLMgPFBxLg7PYHFGw04I1JXRg3ah3an1ABMLyKvNWH0C3zcfwpfmh8kUvFoULxTEJADVSdNrrcqSsaABmQXAN7lRxi1y-fhzQFSGVBOWQ6FaTla+71AAsBqNyFFznF5r4UtxznB8sVMLRJyxiP4yNR6MxZxxDqAA\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Try it out on playground<\/strong><\/a><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Rassicurante, non \u00e8 vero?<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\">Const assertion (array of values as a union type)<\/h3>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Union types sure are handy, but there\u2019s one significant downside to them: being a part of static typing, any data about the possible values a union type allows is lost during the compilation process and can\u2019t be used at runtime. This means, among other things, that you can\u2019t loop over the values of a union type, and there\u2019s no way to check whether an untrusted value is allowed.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">If your project needs a group of values you can use both statically and dynamically, you\u2019re most likely better off using an <a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/enums.html\">enum<\/a>. TypeScript enums allow you to define a group of named constants which are used by the compiler to check for the validity of types and, unlike union types, are also available at runtime.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">However, a similar result can also be achieved with an array of scalar values, like an array of strings. To do that, you need to take advantage of the <a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/release-notes\/typescript-3-4.html#const-assertions\">const assertion<\/a> construct. What is that, you may ask. Why, thanks for the question, that\u2019s just what I\u2019m here for.So: whenever you declare and initialize a variable without explicitly typing it, TypeScript will infer an adequate type for said variable on your behalf according to the provided value. An array of strings will therefore be typed as a <strong>string[]<\/strong>. You can check that out yourself in the Playground: just click on the \u201c.d.ts\u201d tab.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-syntaxhighlighter-code\">const array = [\"string_1\", \"string_2\", \"string_3\"];\r\n\r\n\/\/ Inferred type:\r\n\/\/ declare const array: string[];<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.typescriptlang.org\/play?ssl=4&amp;ssc=34&amp;pln=1&amp;pc=1#code\/MYewdgzgLgBAhgJwXAnjAvDA2gImggSzAHMB9ARhwBoY8pCTSAma2-IsgZhwF0BuALAAoYQHpRMAJJgAZgFMkcgCYwoKAA5yAXGIlK5wADaI5MUJFiJkKLTHYks-IA\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Try it out on playground<\/strong><\/a><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">By using a const assertion, you can control this behavior. Const assertions ask TypeScript to infer the most restrictive type possible given the provided value. Thus, an array of strings initialized with a list of values will be typed as a read-only array, with a specific length, which contains specifically the provided values, each of them at a specific index &#8211; kind of a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Tuple\">tuple<\/a>, if you will.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-syntaxhighlighter-code\">const array = [\"string_1\", \"string_2\", \"string_3\"] as const;\r\n\r\n\/\/ Inferred type:\r\n\/\/ declare const array: readonly [\"string_1\", \"string_2\", \"string_3\"];<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.typescriptlang.org\/play?#code\/MYewdgzgLgBAhgJwXAnjAvDA2gImggSzAHMB9ARhwBoY8pCTSAma2-IsgZhwF14IYoSFADcAWABQkgPTSYASTAAzAKZIVAExhQUABxUAuGXI0rgAG0QrB4aPCSoDMBCrgbw5tLnaNKNOgxkLP4+XLwiQA\"><strong>Try it out on playground<\/strong><\/a><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Such an array can still be used in the code, to loop over the values inside or to check if some other value appears in the list, but thanks to the const assertion you can also create a union type out of it.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-syntaxhighlighter-code highlighted-error\">const someArray = [\"string_1\", \"string_2\", \"string_3\"] as const;\r\n\r\ntype SomeUnion = typeof someArray[number];\r\n\r\n\/\/ Valid:\r\nconst someVar1: SomeUnion = \"string_1\";\r\n\r\n\/\/ Not valid:\r\nconst someVar2: SomeUnion = \"string_5\";\r\n\r\n\/\/ You can still use someArray as a regular array:\r\nif (someArray.indexOf(\"string_1\") !== -1) {\r\n  console.log(\"This is valid!\");\r\n}\r\nfor (let i = 0; i &lt; someArray.length; i++) {\r\n  console.log(someArray[i]);\r\n}<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.typescriptlang.org\/play?#code\/MYewdgzgLgBBIFsCmBBATmghgTxgXhgG0AiaNASzAHNMB9ARmIBoZSoLq6AmZ1symrQDMxALoxMEGKEhQA3AFgAUMqjYADkhgBlREgCqYcuHww1mkADM4e9FmyEwAVwQAjJGlGKVSgPS+YADVMABtyABMQAC5lGWgbZGC0eiidPUNjMFM2DkFGb2V-GAA5EwA3UIjo2PB4+ETMNC5U3WQMkwIcgToAVmICvwCABRAIeGlEJzAARyctJwhGrXrUDBwJ5Ak17BilcmsAChW7HAA6SnCkAA8AeUsDrs4GYgBKGABCPAIAWno3gG9lDAJpAQCEkKcQiAqA8ACrkdQgGAVMKRV7eAC+yksIDQMAO4Ng5FMAAY5DBiQAeBKreyQpDUKAAC3J5AA1GyAUCQfBwZDoUdbNtCORRC9MUA\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Try it out on playground<\/strong><\/a><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">This lets you combine the pros of having an array of values, really useful if you need <a href=\"https:\/\/github.com\/jquense\/yup\">Yup<\/a> validation for instance, and all the good stuff coming from having a union type, like static typing for property values and autocompletion from your IDE.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\">Template\/Dynamic union types<\/h3>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Wow, yet another tip on union types! What were the odds?<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">As we already saw, union types can be used in order to create variables or properties that can take one of a list of scalar values.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-syntaxhighlighter-code highlighted-error\">\/\/ Union type:\r\ntype SomeUnion = \"string_1\" | \"string_2\" | \"string_3\";\r\n\r\n\/\/ Valid:\r\nconst someVariable1: SomeUnion = \"string_1\";\r\n\r\n\/\/ Not valid:\r\nconst someVariable2: SomeUnion = \"string_5\";<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.typescriptlang.org\/play?ssl=8&amp;ssc=45&amp;pln=1&amp;pc=1#code\/PTAEFUDsEsHtNAFwJ4AcCmAuAsAKBRqAMqwC26UcCAvKAEQDOiATtJAOYD6AjHaAD70mrDpwBMfQYxZsuAZjoBuPHhCgAagEMANtAAmOXAGN4TUAzLotrTQCNt6bpmKXK8ULWkiuvZblVgAHKwiKAAbjr6hiaQZhbk1tB2DmLOJORuNEIyogCsSkA\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Try it out on playground<\/strong><\/a><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">This is useful, but kind of limiting, since you can only use the values in the list with no variation whatsoever. Everything that\u2019s not part of the list will cause a type error and TypeScript will refuse to compile.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Now, let\u2019s say you have a string that can take one value from a list that includes a bunch of very similar but not entirely static values. For instance, you may have an algorithm which makes a few attempts to perform a task that may fail, and you want to create a variable to keep the status of the algorithm.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Let\u2019s create a <strong>status<\/strong> variable, its possible values being:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><strong>none<\/strong> when no attempt was performed;<\/li>\r\n\r\n\r\n\r\n<li><strong>success<\/strong> if the task was successfully completed;<\/li>\r\n\r\n\r\n\r\n<li><strong>fail<\/strong> if the task failed;<\/li>\r\n\r\n\r\n\r\n<li><strong>attempt_N<\/strong>, with N being the counter for the current attempt (<strong>attempt_1<\/strong>, <strong>attempt_2<\/strong>, <strong>attempt_3\u2026<\/strong>), while the algorithm is running.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Using union types the old-fashioned way, you may end up explicitly writing down the static values and just leaving a generic <em>string<\/em> for the last ones\u2026<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-syntaxhighlighter-code\">\/\/ Union type for the status:\r\ntype StatusType = \"none\" | \"success\" | \"fail\" | string;\r\n\r\n\/\/ This is valid...\r\nconst status1: StatusType = \"none\";\r\nconst status2: StatusType = \"attempt_3\";\r\n\r\n\/\/ ...but this is valid as well.\r\nconst status3: StatusType = \"Hey, I'm not a status at all!\";<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.typescriptlang.org\/play?#code\/PTAEFUDsEsHtNAFwJ4AcCmoBmsBOSALTAZ0QENEBXYgLgFgAoFDUAZXKuIBU1MBeUACJI8dINAAfIcUoBjWemLFxUwVjLQANitClc0SAHMA3I0YhQXAtGKgboAG5lN0ACYA6T41nxSujtQAjDRsAdy8oALCooKmDD6QfqQU1ABMIewp4SxRFIjoALaoiAD6AMyxZgwWnu4ARpSIhPb2Ti6uoGS2AO7ompru3r5NyZxlGWE8OUIAEujIADSgAJIA5AWgIk1k-lmd2-0AhLFAA\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Try it out on playground<\/strong><\/a><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">\u2026but that\u2019s not the only way. The string values defined for a union type can actually have dynamic parts, which we can control by simply concatenating dynamic parts and static parts with <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Template_literals\">template strings<\/a>.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-syntaxhighlighter-code highlighted-error\">\/\/ Union type for the status:\r\ntype StatusType = \"none\" | \"success\" | \"fail\" | `attempt_${number}`;\r\n\r\n\/\/ This is valid...\r\nconst status1: StatusType = \"none\";\r\nconst status2: StatusType = \"attempt_3\";\r\n\r\n\/\/ ...but this one isn't.\r\nconst status3: StatusType = \"Aw :(\";<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.typescriptlang.org\/play?ssl=9&amp;ssc=37&amp;pln=1&amp;pc=1#code\/PTAEFUDsEsHtNAFwJ4AcCmoBmsBOSALTAZ0QENEBXYgLgFgAoFDUAZXKuIBU1MBeUACJI8dINAAfIcUoBjWemLFxUwVjLQANitAADConQBbVIgD6AEgDekSkYBG6XAF9dAbkaMQoLgWjFQf1AANzJNaAATADoYxll4UlBSCmoARho2DmoeFgFhUUEPBnjIROTOACYM9hTuXlA8g2NTMwBmQs8Gbxio+0pEQiDRQOJIAHJEKLiEgfLqVuqsutyhAEEAd1AaAApCoA\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Try it out on playground<\/strong><\/a><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Further examples can be found <a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/2\/template-literal-types.html\">here<\/a>.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">We love you, union types &lt;3<\/p>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\">Closing words<\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Approaching TypeScript is an interesting experience for a developer used to traditional object-oriented programming languages. Being it a superset of a non-statically typed language such as JavaScript, the team developing it was able to find a lot of ways to tackle every sort of issue, and even after some time working with it you still happen to discover something new every once in a while.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">I presented to you some features I found out to be useful during my work, hoping they may help you too. I\u2019m sure I\u2019ll find more of them in the future.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">What do you think? Did you actually learn anything new, or did you already know about every tip? Do you have any of your own tips you\u2019d like to show to the rest of us? Let me know, and maybe one day I\u2019ll make a sequel!<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.oimmei.com\/en\/about-us\/\" target=\"_blank\" rel=\"noreferrer noopener\">Andrea Cioni<\/a><\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>We planned to release this article a long time ago, but we had some issues compiling it. We finally solved every type error, so here we go. TypeScript Oh, TypeScript. The open source programming language developed by Microsoft as a superset of JavaScript doesn\u2019t need any introduction at this point. With tools like React, Angular [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":25736,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[91],"tags":[],"class_list":["post-18625","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-development-en"],"acf":[],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.2.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"We planned to release this article a long time ago, but we had some issues compiling it. We finally solved every type error, so here we go. TypeScript Oh, TypeScript. The open source programming language developed by Microsoft as a superset of JavaScript doesn\u2019t need any introduction at this point. With tools like React, Angular\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Oimmei Team\"\/>\n\t<meta name=\"google-site-verification\" content=\"nrov3ZDmFh2g122GMXaTWPiPrzv3uAspJFs_42s7-6s\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.oimmei.com\/en\/typescript-5-tricks-development\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.2.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Oimmei Technologies | The Boutique Agency\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"5 useful TypeScript tips &amp; tricks | Oimmei Technologies\" \/>\n\t\t<meta property=\"og:description\" content=\"We planned to release this article a long time ago, but we had some issues compiling it. We finally solved every type error, so here we go. TypeScript Oh, TypeScript. The open source programming language developed by Microsoft as a superset of JavaScript doesn\u2019t need any introduction at this point. With tools like React, Angular\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.oimmei.com\/en\/typescript-5-tricks-development\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/www.oimmei.com\/wp-content\/uploads\/2026\/08\/oimmei-cover-website-26-scaled.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/www.oimmei.com\/wp-content\/uploads\/2026\/08\/oimmei-cover-website-26-scaled.png\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2022-12-21T09:26:26+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2025-06-03T13:13:40+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"5 useful TypeScript tips &amp; tricks | Oimmei Technologies\" \/>\n\t\t<meta name=\"twitter:description\" content=\"We planned to release this article a long time ago, but we had some issues compiling it. We finally solved every type error, so here we go. TypeScript Oh, TypeScript. The open source programming language developed by Microsoft as a superset of JavaScript doesn\u2019t need any introduction at this point. With tools like React, Angular\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/www.oimmei.com\/wp-content\/uploads\/2026\/08\/oimmei-cover-website-26-x.png\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"5 useful TypeScript tips & tricks | Oimmei Technologies","description":"We planned to release this article a long time ago, but we had some issues compiling it. We finally solved every type error, so here we go. TypeScript Oh, TypeScript. The open source programming language developed by Microsoft as a superset of JavaScript doesn\u2019t need any introduction at this point. With tools like React, Angular","canonical_url":"https:\/\/www.oimmei.com\/en\/typescript-5-tricks-development\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"google-site-verification":"nrov3ZDmFh2g122GMXaTWPiPrzv3uAspJFs_42s7-6s","miscellaneous":""},"schema":null,"og:locale":"en_US","og:site_name":"Oimmei Technologies | The Boutique Agency","og:type":"article","og:title":"5 useful TypeScript tips &amp; tricks | Oimmei Technologies","og:description":"We planned to release this article a long time ago, but we had some issues compiling it. We finally solved every type error, so here we go. TypeScript Oh, TypeScript. The open source programming language developed by Microsoft as a superset of JavaScript doesn\u2019t need any introduction at this point. With tools like React, Angular","og:url":"https:\/\/www.oimmei.com\/en\/typescript-5-tricks-development\/","og:image":"https:\/\/www.oimmei.com\/wp-content\/uploads\/2026\/08\/oimmei-cover-website-26-scaled.png","og:image:secure_url":"https:\/\/www.oimmei.com\/wp-content\/uploads\/2026\/08\/oimmei-cover-website-26-scaled.png","article:published_time":"2022-12-21T09:26:26+00:00","article:modified_time":"2025-06-03T13:13:40+00:00","twitter:card":"summary","twitter:title":"5 useful TypeScript tips &amp; tricks | Oimmei Technologies","twitter:description":"We planned to release this article a long time ago, but we had some issues compiling it. We finally solved every type error, so here we go. TypeScript Oh, TypeScript. The open source programming language developed by Microsoft as a superset of JavaScript doesn\u2019t need any introduction at this point. With tools like React, Angular","twitter:image":"https:\/\/www.oimmei.com\/wp-content\/uploads\/2026\/08\/oimmei-cover-website-26-x.png"},"aioseo_meta_data":{"post_id":"18625","title":null,"description":null,"keywords":null,"keyphrases":{"focus":{"keyphrase":"","score":0,"analysis":{"keyphraseInTitle":{"score":0,"maxScore":9,"error":1}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"BlogPosting","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2025-03-15 22:39:36","updated":"2025-06-07 19:34:29","seo_analyzer_scan_date":null,"focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.oimmei.com\/en\/\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">|<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.oimmei.com\/en\/category\/software-development-en\/\" title=\"Software Development\">Software Development<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">|<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t5 useful TypeScript tips &amp; tricks\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.oimmei.com\/en\/"},{"label":"Software Development","link":"https:\/\/www.oimmei.com\/en\/category\/software-development-en\/"},{"label":"5 useful TypeScript tips &amp; tricks","link":"https:\/\/www.oimmei.com\/en\/typescript-5-tricks-development\/"}],"_links":{"self":[{"href":"https:\/\/www.oimmei.com\/en\/wp-json\/wp\/v2\/posts\/18625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oimmei.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oimmei.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oimmei.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oimmei.com\/en\/wp-json\/wp\/v2\/comments?post=18625"}],"version-history":[{"count":0,"href":"https:\/\/www.oimmei.com\/en\/wp-json\/wp\/v2\/posts\/18625\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.oimmei.com\/en\/wp-json\/wp\/v2\/media\/25736"}],"wp:attachment":[{"href":"https:\/\/www.oimmei.com\/en\/wp-json\/wp\/v2\/media?parent=18625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oimmei.com\/en\/wp-json\/wp\/v2\/categories?post=18625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oimmei.com\/en\/wp-json\/wp\/v2\/tags?post=18625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}