<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: dade!</title>
    <description>The latest articles on DEV Community by dade! (@dadepo).</description>
    <link>https://dev.to/dadepo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F52048%2Fafa5cd3c-653d-4ba6-81cb-9e6d07c8c738.png</url>
      <title>DEV Community: dade!</title>
      <link>https://dev.to/dadepo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dadepo"/>
    <language>en</language>
    <item>
      <title>Why I am offering NFTs as a crowdfunding reward</title>
      <dc:creator>dade!</dc:creator>
      <pubDate>Mon, 13 Sep 2021 12:00:49 +0000</pubDate>
      <link>https://dev.to/dadepo/why-i-am-offering-nfts-as-a-crowdfunding-reward-2054</link>
      <guid>https://dev.to/dadepo/why-i-am-offering-nfts-as-a-crowdfunding-reward-2054</guid>
      <description>&lt;p&gt;Non-equity funding campaigns are always based on promises: "Back us now, and we promise you early access to the product when we launch, or we promise you a discount, digital shout-out or swags". I am currently running an Indiegogo campaign where the promise will also include an NFT: Non Fungible Token, which is a digital artifact that can be used to prove things like ownership. This, as far as I know, is the first time NFTs would be used in this capacity.&lt;/p&gt;

&lt;p&gt;In this post, I will shed some light on what NFTs are and share my thoughts on why they are a  perfect perk for a non-equity crowdfunding campaign. In doing so, I will touch on my thoughts on the continual march towards the digitalization of the human experience. How digitalization and technologies like virtual/augmented reality, blockchain technology, etc are making it possible to further digitize aspects of society and human interaction that were previously rife with problems or just plain impossible to digitize.&lt;/p&gt;

&lt;p&gt;By the way, my campaign is to kickstart the development of DishAfrik: a recipe app to discover, enjoy and share African recipes. Think of it as Yummly or Tasty.co but focused on African cuisine. One of the perks of the campaign is going to be 54 uniquely generated collages made up of 54 recipes from 54 countries. 54 because currently Africa is home to 54 fully recognised sovereign states. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You should check out the IndieGoGo campaign and support the project. Find the link &lt;a href="https://igg.me/at/dishafrik"&gt;here&lt;/a&gt; :)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;TL;dr&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ever since the third industrial revolution, there has been a march towards digitalization of the physical world.&lt;/li&gt;
&lt;li&gt;Blockchain-related technologies, like NFTs, provided the possibility to digitize aspects of the physical world that relied on scarcity, uniqueness, and proof of ownership, aspects that were previously hard to digitize. &lt;/li&gt;
&lt;li&gt;DishAfrik's Indiegogo campaign will be making use of NFTs as a perk, using it as a way to capture participation, while also creating digital art in the form of a recipe collage that can also be owned.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Ever before The Metaverse
&lt;/h2&gt;

&lt;p&gt;It started with the Third Industrial Revolution, often referred to as the Digital Revolution. A period that began in the late 1900s that led to the spread of automation and digitization through the use of electronics and computers. &lt;/p&gt;

&lt;p&gt;The technological invention of this era made it possible for things that once existed only in the tactile realm to now also exist in a new and alternative realm: the digital world. &lt;/p&gt;

&lt;p&gt;Obviously, such powerful technology would have an impact on society. It was not enough to have good old mails, we got new ones with an e as a prefix: e-mails. The music industry had its Napster scare, and then we had the spotify-cation of music distribution, which killed the CD, at whose hands, cassette and VHS suffered similar fatal fate. Journalism, Medicine, Education, Entertainment you name it, all got impacted by this continual digitalization wave.&lt;/p&gt;

&lt;p&gt;Even with all these seismic shifts that digitalization brought, there were certain aspects of society and human interactions that just could not be properly represented in the digital world. This is because these human interactions are inherently at odds with the very nature of digitalization.&lt;/p&gt;

&lt;p&gt;How would it be possible to properly digitize commerce for instance? How would it be possible to create e-money just like e-mail? Electronic mail is not problematic, because duplicating a message and sending it multiple times does not in any way compromise the objective of communication which email serves, the same can't be said of a naively designed digital version of money. &lt;/p&gt;

&lt;p&gt;If the same endless duplication is possible with digital money, what is stopping someone from making multiple copies of a digital coin, to spend the coin multiple times? Being able to do this negates the ability to carry out commerce in the digital world. This problem, by the way, is called the double-spend problem. &lt;/p&gt;

&lt;h2&gt;
  
  
  Trusted third parties, bottlenecks, and anarchists.
&lt;/h2&gt;

&lt;p&gt;To enable commerce on the internet, we resulted into having trusted third parties who mediate the commercial exchange. This is not a new thing. The antithesis of anything digital when it comes to commerce, the paper money, originated in China in the 7th century, on the back of trusted third parties. &lt;/p&gt;

&lt;p&gt;Before the use of these notes, the Chinese used circular copper coins. If you were wealthy, these copper coins soon became a burden to carry around, especially when used for larger transactions. To solve this problem, the coins could be deposited with a trusted person, who then gave the wealthy merchant a slip of paper (the receipt) that says how much money they had deposited. This slip of paper was then used in commercial activities in the exchange of goods and services. When the recipient of the paper slip returns to the trusted party, they could then exchange the slip of paper for the actual coins.&lt;/p&gt;

&lt;p&gt;In the hundreds of years between 7th century China and the 20th century, human monetary affairs came to follow the Chinese model. Therefore it is no surprise that a model involving trusted third parties was also employed when commerce first moved online. In this model, the trusted party serves as the electronic mint that creates and tracks every digital coin that is being spent. In doing so, they can prove ownership and who is allowed to spend what coin, when it is spent, and that it can't be spent twice.&lt;/p&gt;

&lt;p&gt;The first successful foray into digitizing money was &lt;a href="https://en.wikipedia.org/wiki/E-gold"&gt;E-gold&lt;/a&gt;. E-gold was a digital gold currency run by Gold &amp;amp; Silver Reserve Inc, which served as trusted third parties. In this capacity they enabled people to transact business online by moving computer bits from one location to another with the assurance that transaction accounting was handled by the E-gold system and the actual money would be made available to parties when requested. &lt;/p&gt;

&lt;p&gt;So with this model, digitalization of commerce, tracking, and transferring anything of value was made possible. Finally! Well, maybe not quite.&lt;/p&gt;

&lt;p&gt;There is something not quite kosher, with a model involving a trusted third party: and that is the need to have a trusted third party. As the saying goes, absolute power corrupts absolutely. Trusted parties usually occupy a unique role that bestows upon them, if not absolute power, the closest thing to it. &lt;/p&gt;

&lt;p&gt;Apart from the political schools of thought that would rather get rid of trusted third parties, there are other weaknesses to relying on trusted third parties. For one they are usually a single point of failure, or point of corruption, depending on the situation. They can also be bottlenecks as their inefficiencies can have rippling effects. Trusted third parties are also by their very nature centralized, and centralized systems are generally harder to scale. The internet would never have evolved into what it is today if it was centralized.&lt;/p&gt;

&lt;p&gt;What then do we have? We are left with a deceleration in this march towards the digitalization of everything previously analog. The best we could come up with left us with an internet, where commerce, that quintessential human activity, could not be liberated from trusted third parties. It was also a state where concepts like ownership could not be fully grafted onto the digital world.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cryptography wins again!
&lt;/h2&gt;

&lt;p&gt;The genius of Satoshi Nakamoto, the creator of blockchain was in conceiving a protocol that relied on cryptographic primitives to mediate the transfer of value without requiring the services of trusted third parties. Instead of trust, the cryptographic proof is relied upon. This creation is often referred to as the blockchain.&lt;/p&gt;

&lt;p&gt;The blockchain is often seen as a synonym for bitcoin, which is not surprising, as bitcoin was the first implementation of the blockchain. But the blockchain is more than bitcoin. At the most fundamental level, the blockchain can be seen as a distributed ledger. The protocol outlined by Satoshi Nakamoto made it possible to have a distributed ledger that can track ownership and the transfer of ownership of digital artifacts, without needing a centralized repository. Instead of trusted third parties, a peer-to-peer, decentralized model is used, which relies on cryptographic proofs to keep the integrity of the ledger.&lt;/p&gt;

&lt;p&gt;This means, instead of relying on centralized, trusted third parties to keep records of ownership and transfer of ownership of digital artifacts, we rely on cryptography instead. Making it possible to put information in a distributed ledger where everyone and anyone can see its contents, inspect and verify. This means once a transfer of ownership is reflected in the ledger, another attempt to invoke the same ownership transfer would be visible to all relevant parties involved and declined. &lt;/p&gt;

&lt;p&gt;This invention made it possible to truly elevate money into the digital realm. Usage of physical money has never had to deal with the problem of double-spending, due to the very nature of it being physical. Once a coin goes from one hand to another, it is impossible for the former holder to still have the exact coin and attempt to transfer it to someone else. This was possible in the digital realm and for a long time, having a mediating third party that serves as a mint, was the only solution. Blockchain created a system that was able to impose constraints similar to those that come with physical money on digital currency, while avoiding the centralization trap. The protocol was able to achieve this without the need to have a trusted third party.   &lt;/p&gt;

&lt;p&gt;Satoshi Nakamoto's outline of the blockchain was just the spark that ignited other ideas that have led to the emergence of the cryptocurrency/blockchain industry. Once it became clear how to transfer digital artifacts between parties, the next step was to make this programmable. And this is exactly the innovation Ethereum brought into the space. &lt;/p&gt;

&lt;p&gt;Ethereum created a computation layer on top of the blockchain, making it possible to encode autonomous rules that control how digital artifacts are to be moved between parties on top of the blockchain. This layer of computation on the blockchain is often referred to as smart contracts. Even though it was coined in the 1990s, by Nick Szabo, arguably Ethereum was what made the term smart contract mainstream.&lt;/p&gt;

&lt;p&gt;Another important idea that proved to be critical is the idea of permanence. There are cases where information is required to be unmodifiable. For example, the details of a contract for instance should be unmodifiable after the fact. Unfortunately digital information cannot generally guarantee such a constraint. Digital information is by nature modifiable. &lt;/p&gt;

&lt;p&gt;Permanence is one of the central value propositions of the blockchain. If you think about this, it makes sense. A technology that records transactions of value should not allow rewriting of history. Information contained on the blockchain can be considered permanent because the way the protocol is assembled, again making use of cryptography, is such that the cost of going back to change any information on it is so great that it is effectively impossible.&lt;/p&gt;

&lt;p&gt;The only snag is, the blockchain is not designed to be a store of arbitrary large data. It does not scale for such a use case. This is where the idea of a permanent web comes into the picture, which, simply put, is a web where contents are immutable. That is, once content is created it should not be editable. &lt;/p&gt;

&lt;p&gt;The current web is mutable. Digital content at a particular URL can be changed even though the URL remains the same. Having such mutability is at odds with the need to make statements about permanent events on the web. For example, a contract between two parties found at a particular URL provides less value when there is no guarantee that the content at the URL would always stay the same. A permanent web provides such guarantees.&lt;/p&gt;

&lt;p&gt;There has been, and continues to be many attempts at implementing such a permanent web. Arguably, the effort that has been enjoying the most success is IPFS. Which is not only a permanent web but also a distributed web. With IPFS, it is possible to offload large data that needs to be immutable out of the blockchain to the permanent web and have only a reference stored on the blockchain.    &lt;/p&gt;

&lt;p&gt;The appearance of these technologies: blockchain, smart contracts, permanent web, etc has made it possible for us to continue with the onward march of digitizing our world. These technologies have made it possible to replicate the properties of physical items like scarcity, uniqueness, and proof of ownership in the digital world. Properties that were previously hard to digitize.&lt;/p&gt;

&lt;h2&gt;
  
  
  Emergence of the NFT
&lt;/h2&gt;

&lt;p&gt;Now that we have these technologies, as is usually the case, thanks to human ingenuity, what has followed is the realization that these technologies can be put to use in clever ways. We have since seen their applications in the financial industry (Defi — Decentralized finance), to new and creative ways organization can be managed (DAO — Distributed Autonomous Organisation), to applications in the supply chain industry.&lt;/p&gt;

&lt;p&gt;How blockchain and smart contracts can be used is almost endless and rightly so. This is because a lot of society and human interaction involves dealing with scarcity, uniqueness of things, ownership, and transferring of value. Once we had the means to represent these in the digital world, the possibilities became endless. &lt;/p&gt;

&lt;p&gt;A recent application of blockchain and smart contracts that succinctly captures this is the emergence of what is now known as NFTs (Non-Fungible tokens), which have proven to be particularly germane in the creative — music, art, collectible — industry.&lt;/p&gt;

&lt;h2&gt;
  
  
  What exactly are NFTs?
&lt;/h2&gt;

&lt;p&gt;Let's start with what a token is. A token can be said to be a digital artifact that could either exist 100% in the digital world, without any physical counterpart or a digital representation of something physical. &lt;/p&gt;

&lt;p&gt;A token could represent currency, lottery tickets, shares in a company, reputation points in an online platform, title deeds, etc. &lt;/p&gt;

&lt;p&gt;Tokens could also be of two kinds. They are either fungible or non-fungible. A fungible token is a digital artifact without identity and hence not unique. Non-fungible tokens on the other hand are inherently unique because they possess an inherent identity.&lt;/p&gt;

&lt;p&gt;To illustrate: A 5 dollar bill is as good as the next 5 dollar bill. This is because each banknote does not function on the basis of a unique identity, hence they are fungible. Which is another way of saying they are replaceable with one another, as long as their values are the same. Some other artifacts, on the other hand, carry intrinsic identity and can't just be replaced by another. For example a birth certificate, if tokenized, would be non-fungible. &lt;/p&gt;

&lt;p&gt;An NFT is then a unique, digital artifact that can exist on a blockchain.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why NFTs?
&lt;/h2&gt;

&lt;p&gt;It turns out there are a lot of things in the physical world that are valuable because of their non-fungibility. Being able to capture this and represent it in the digital world is what makes an NFT such a valuable piece in the crypto space.&lt;/p&gt;

&lt;p&gt;In the digital art sphere, an artist can create a work of art, sell it to a buyer and issue an NFT that can be used to prove ownership of that unique work of art. &lt;/p&gt;

&lt;p&gt;In the gaming industry, NFTs can be used to represent and prove ownership of unique in-game items. &lt;/p&gt;

&lt;p&gt;In the music industry, an artist can issue a unique NFT to everyone who buys an album. Such an NFT can then be used to prove ownership of purchase and/or to unlock extra content. &lt;/p&gt;

&lt;p&gt;NFTs can also be used to create digital collectibles as well as memorabilia, in which case the NFT can be used to capture and document the authenticity of a memorable event.  &lt;/p&gt;

&lt;p&gt;NFTs can also be used as perks in crowdfunding campaigns. This is exactly what the DishAfrik NFT will do. &lt;/p&gt;

&lt;h2&gt;
  
  
  DishAfrik's NFT perk
&lt;/h2&gt;

&lt;p&gt;As earlier stated, I am currently running an IndieGoGo campaign to kickstart the development of DishAfrik: a recipe app to discover, enjoy and share African recipes.&lt;/p&gt;

&lt;p&gt;One of the perks for backers of the Indiegogo campaign will be an NFT. The NFT will be a uniquely generated image collage (similar to Beeple’s &lt;a href="https://en.wikipedia.org/wiki/Everydays:_the_First_5000_Days"&gt;The First 5000 Days&lt;/a&gt;). The NFT will consist of pictures of 54 recipes from 54 African countries. The ingredients and cooking steps will be included as part of the NTTs meta-data.&lt;/p&gt;

&lt;p&gt;The DishAfrik NFT perk will serve as a means for capturing participation in an activity (in this case the backing of a non-equity fundraising campaign) and encode that in the digital world. Recipients of the NFT perk will be able to prove that they indeed backed the Indiegogo campaign. &lt;/p&gt;

&lt;p&gt;The NFT will also be a digital art that can be owned: a picture made up of a collage of 54 recipes from 54 African countries. &lt;/p&gt;

&lt;p&gt;As the IndieGoGo campaign will be an event in time, the NFT will also have memorabilia value.  Who knows what this will be worth in the coming years?&lt;/p&gt;

&lt;p&gt;Participation in an event, prove-able ownership of art, and memorabilia artifacts were all things that existed without hassle in the physical world before now. But with the advent of new technologies that further the convergence of the physical and digital world, it is now possible to represent these things also in the digital world. &lt;/p&gt;

&lt;p&gt;This is why the DishAfrik NFT perk exists. &lt;/p&gt;

&lt;h2&gt;
  
  
  Metaverse, virtual and augmented reality
&lt;/h2&gt;

&lt;p&gt;This convergence of the physical and digital world that started with the digital revolution shows no sign of abating. One can say we are on the cusp of breakthroughs in these endeavors. Technologies within the space of virtual and augmented reality are exploring ways to further translate other unique physical attributes like "presence" into the digital world.  &lt;/p&gt;

&lt;p&gt;Mark Zuckerberg thinks this is the future. And spurred on by this belief he has laid out his intention to &lt;a href="https://www.theverge.com/22588022/mark-zuckerberg-facebook-ceo-metaverse-interview"&gt;transform Facebook into a metaverse company&lt;/a&gt;. This would turn Facebook into a place where there is the convergence of physical, augmented, and virtual reality in a shared online space.&lt;/p&gt;

&lt;p&gt;Although we do not have such a metaverse yet, and it might take a couple more years before its full realization, we do have technologies like NFTs now. And because of that, there is a campaign to build a platform to showcase African cuisine that if you back, you have an option of owning an NFT. &lt;/p&gt;

&lt;p&gt;So you should &lt;a href="https://igg.me/at/dishafrik"&gt;go check that out now&lt;/a&gt;!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>First impressions with Deno from building PlanetTypeScript.com</title>
      <dc:creator>dade!</dc:creator>
      <pubDate>Mon, 07 Jun 2021 20:18:27 +0000</pubDate>
      <link>https://dev.to/dadepo/first-impressions-with-deno-from-building-planettypescript-com-1k1n</link>
      <guid>https://dev.to/dadepo/first-impressions-with-deno-from-building-planettypescript-com-1k1n</guid>
      <description>&lt;p&gt;&lt;a href="https://www.planettypescript.com"&gt;PlanetTypeScript.com&lt;/a&gt; is a content aggregator for all things TypeScript. It regularly polls RSS feeds for TypeScript related content, which it then list on the site, send out as a tweet via &lt;a href="https://twitter.com/planetypescript"&gt;@planetypescript&lt;/a&gt;, and also as a weekly digest to subscribers.&lt;/p&gt;

&lt;p&gt;Back when I used to write a lot of Java-related content on this blog, I found out that my blog ended up on &lt;a href="http://www.topjavablogs.com/"&gt;www.topjavablogs.com/&lt;/a&gt;, which is a site that aggregates Java contents. So when I decided to play around with Deno, I thought a nice little idea would be to build something similar to topjavablogs.com, but for TypeScript. And that is how &lt;a href="https://www.planettypescript.com"&gt;PlanetTypeScript.com&lt;/a&gt; was born.&lt;/p&gt;

&lt;p&gt;In this post, I will share some of my first impressions with Deno. The things I enjoyed and the bits I found inconvenient. Let's start with the cool stuff.&lt;/p&gt;

&lt;h3&gt;
  
  
  Native TypeScript Support
&lt;/h3&gt;

&lt;p&gt;I find it super convenient that I could just write TypeScript without worrying about setting up a build chain that would take care of the &lt;em&gt;transpilation&lt;/em&gt; to JavaScript. This is really nice and it makes TypeScript feel more like a standalone language in its own rights. This has to be one of the top-selling points of Deno for me.&lt;/p&gt;

&lt;h3&gt;
  
  
  Built-in utilities
&lt;/h3&gt;

&lt;p&gt;The deno CLI comes with a whole lot of useful utilities and just comes in handy. For example &lt;code&gt;deno info&lt;/code&gt; to check the dependencies. &lt;code&gt;deno fmt&lt;/code&gt;: a code formatter,  &lt;code&gt;deno bundle&lt;/code&gt; for bundling modules and dependencies into a single file, &lt;code&gt;deno compile&lt;/code&gt; for creating a self-contained executable, etc.&lt;/p&gt;

&lt;p&gt;I personally find dealing with build pipelines within the JavaScript ecosystem to be a tiring chore. Having something like &lt;code&gt;deno bundle&lt;/code&gt; or &lt;code&gt;deno compile&lt;/code&gt;, built-in is super useful, as it takes care of most of the heavy lifting when it comes to deployment.&lt;/p&gt;

&lt;h3&gt;
  
  
  Web API
&lt;/h3&gt;

&lt;p&gt;Deno seeks to provide implementations of Web API, and this I found to be really handy. For &lt;a href="https://www.planettypescript.com"&gt;PlanetTypeScript.com&lt;/a&gt;, I need to make HTTP requests and also run background tasks. I could use &lt;code&gt;fetch&lt;/code&gt; and &lt;code&gt;web workers&lt;/code&gt; for this. API's I am already familiar with coming from the Web, hence no need to install third-party libraries. This is super convenient.&lt;/p&gt;

&lt;h3&gt;
  
  
  Good Standard Library
&lt;/h3&gt;

&lt;p&gt;In the course of building &lt;a href="https://www.planettypescript.com"&gt;PlanetTypeScript.com&lt;/a&gt;, I needed to have logging, base64 encoding, use hash functions, and generating UUIDs. I was able to find modules within the Deno standard library that I could use for all of these. The Deno standard library is quite decent and has a battery-included feel to it. You can peruse the available modules here.&lt;/p&gt;

&lt;h3&gt;
  
  
  IDE Support
&lt;/h3&gt;

&lt;p&gt;I primarily use Jetbrains IDEs. When not within a full-blown IDE environment, I use Microsoft Code. Both of these tools have plugins that implement support for Deno and they work very well. I was able to use the visual debugger in IntelliJ, which was quite handy.&lt;/p&gt;

&lt;p&gt;So these are some of the cool things that made a good first impression on me with Deno. Below are some of the things that were a bit of a hassle.&lt;/p&gt;

&lt;h3&gt;
  
  
  No Registry and Package Manager.
&lt;/h3&gt;

&lt;p&gt;Deno does not have any well-known central registry from which a package manager manages dependencies. Instead, Deno works with plain URLs. This setup leads to certain consequences which I believe hamper the developer experience. &lt;/p&gt;

&lt;p&gt;For example, there is no central file to look into, to see all the dependencies of a project. To work around this, the convention in the Deno community is to re-import all dependencies from a file called &lt;code&gt;deps.ts&lt;/code&gt; - basically recreating some sort of &lt;code&gt;package.json&lt;/code&gt; as you have it in &lt;code&gt;npm&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Also, there is no easy way to quickly see if there are new versions of any of my project's dependencies. Or even better still, no command I could run from the command line that updates dependencies. &lt;/p&gt;

&lt;p&gt;I later learned about &lt;a href="https://github.com/crewdevio/Trex"&gt;Trex&lt;/a&gt; and &lt;a href="https://github.com/drashland/dmm"&gt;dmm&lt;/a&gt; which could be used to achieve some of these tasks. I can't comment on them now as I have not gotten around to try them out yet.&lt;/p&gt;

&lt;p&gt;From the perspective of the developer, I am yet to see the advantage the Deno model brings to the table. Instead, it feels to me more like a philosophical design decision, rather than a pragmatic one.&lt;/p&gt;

&lt;h3&gt;
  
  
  Nascent Library Ecosystem.
&lt;/h3&gt;

&lt;p&gt;The inability to find needed third-party libraries proved to be one of the main inconveniences.&lt;/p&gt;

&lt;p&gt;I had issues finding a third-party library to deal with OAuth. I tried &lt;a href="https://github.com/oslabs-beta/dashport"&gt;Dashpart&lt;/a&gt;, but it has become incompatible with recent versions of Deno and &lt;a href="https://oakserver.github.io/oak/"&gt;Oak&lt;/a&gt; (the HTTP middleware library I used). I tried using Firebase-auth, but there is no support for Deno.&lt;/p&gt;

&lt;p&gt;I use MailChimp for sending the Weekly digests. Since the content of the emails would be different weekly, I needed to build the message dynamically. This involves interacting with MailChimp via its API. Unfortunately, MailChimp does not have an API client library for Deno, so I ended up having to directly call the needed HTTP endpoints.&lt;/p&gt;

&lt;p&gt;Deno does have a compatibility layer that makes it possible to use Node/npm modules from within Deno. There are also CDN's like Skypack that make it possible to import npm modules for use within Deno. What I quickly realise is that these workarounds, work best for the simplest of libraries. It does not work for anything slightly complicated. For example, I could not use any of these methods to get the Node library for Firebase-auth nor Mailchimp client library to work.&lt;/p&gt;

&lt;p&gt;This nascency is strictly not a structural issue with Deno and something to be expected, given how new Deno is. This is also the sort of thing that would improve with time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion.
&lt;/h3&gt;

&lt;p&gt;I enjoyed using Deno. Having native TypeScript support, various CLI utilities, and a battery included-standard library, makes it easy to get productive real quick. The main inconveniences I encountered can be chucked up to Deno still being relatively new, but even with that, it is still a hell of a lot usable/productive for a 3 years old project.&lt;/p&gt;

</description>
      <category>deno</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Callback, Promise and Async/Await by Example in JavaScript</title>
      <dc:creator>dade!</dc:creator>
      <pubDate>Thu, 20 May 2021 20:05:38 +0000</pubDate>
      <link>https://dev.to/dadepo/callback-promise-and-async-await-by-example-in-javascript-37ka</link>
      <guid>https://dev.to/dadepo/callback-promise-and-async-await-by-example-in-javascript-37ka</guid>
      <description>&lt;p&gt;This post is going to show, by way of code examples, how to take a callback based API, modify it to use Promises and then use the Async/Await syntax. This post won't go into a detailed explanation of callbacks, promises or the Async/Await syntax. For such a detailed explanation of these concepts please check &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous"&gt;Asynchronous JavaScript&lt;/a&gt;, which is a section of MDN Web Docs, that explains asynchronicity and how callbacks, promises, and the Async/Await syntax helps with working with asynchronous JavaScript.&lt;/p&gt;

&lt;p&gt;This post is meant for the developer who has somewhat of an understanding of asynchronicity in JavaScript, but requires a straight to the point code example to serve as a quick syntax reference for how to take a callback based API, update it to use promises and finally use the Async/Await with it.&lt;/p&gt;

&lt;p&gt;For demonstration purposes, we are going to use the &lt;a href="https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback"&gt;fs.readFile&lt;/a&gt;, which is a callback based API from reading files. We will have a file &lt;code&gt;test.txt&lt;/code&gt; that would contain some text, we will then have a file &lt;code&gt;script.js&lt;/code&gt; that would open the file, read the contents, and print it to the terminal.&lt;/p&gt;

&lt;p&gt;The code will first be implemented using callbacks, it would then be updated to use Promises, and finally, instead of using Promise directly, it will be updated to use Async/Await.&lt;/p&gt;

&lt;p&gt;Let's get started.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using Callbacks
&lt;/h3&gt;

&lt;p&gt;We first create a directory where we are going to work from, create also the file that will contain our code, and the two files that we would be reading from.&lt;/p&gt;

&lt;p&gt;We first create the two files with contents.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; ~/code
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;touch&lt;/span&gt; ~/code/script.js
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Beam me up, Scotty"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; ~/code/test.txt
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; ~/code/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next in the &lt;code&gt;script.js&lt;/code&gt; file, we have the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;readFileCallBack&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./test.txt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;utf8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; [callback]&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;readFileCallBack&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Executing the script by running node &lt;code&gt;script.js&lt;/code&gt; should get "Beam me up, Scotty" printed to the terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;node script.js
Beam me up, Scotty &lt;span class="o"&gt;[&lt;/span&gt;callback]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using Promises
&lt;/h3&gt;

&lt;p&gt;Update &lt;code&gt;script.js&lt;/code&gt; and add a version of &lt;code&gt;readFileCallback&lt;/code&gt; that uses promises. It looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;readFilePromise&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./test.txt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;utf8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
       &lt;span class="k"&gt;return&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;readFilePromise&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
 &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;  &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; [promise]&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
 &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Execute the script by running node &lt;code&gt;script.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;node script.js
Beam me up, Scotty &lt;span class="o"&gt;[&lt;/span&gt;callback]
Beam me up, Scotty &lt;span class="o"&gt;[&lt;/span&gt;promise]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using Async/Await
&lt;/h3&gt;

&lt;p&gt;Update &lt;code&gt;script.js&lt;/code&gt; and add a third version that uses the Async/Await syntax. Since Async/Await is a syntax that makes using promises easier, the Async/Await implementation would make use of the &lt;code&gt;readFilePromise()&lt;/code&gt; function. It looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;readFileAsync&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;readFilePromise&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; [async-await]&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;readFileAsync&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Executing the script by running node &lt;code&gt;script.js&lt;/code&gt; will print something similar to this, to the terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Beam me up, Scotty &lt;span class="o"&gt;[&lt;/span&gt;callback]
Beam me up, Scotty &lt;span class="o"&gt;[&lt;/span&gt;promise]
Beam me up, Scotty &lt;span class="o"&gt;[&lt;/span&gt;async-await]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The complete file with the 3 implementations is presented below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// callback&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;readFileCallBack&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./test.txt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;utf8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; [callback]&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

 &lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;readFileCallBack&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;// promise&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;readFilePromise&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./test.txt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;utf8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
       &lt;span class="k"&gt;return&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="nx"&gt;readFilePromise&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
 &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;  &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; [promise]&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
 &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;


&lt;span class="c1"&gt;// async/await&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;readFileAsync&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;readFilePromise&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; [async-await]&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;readFileAsync&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Error Handling
&lt;/h3&gt;

&lt;p&gt;To illustrate that the error handling in the 3 implementation work as expected, rename the &lt;code&gt;test.txt&lt;/code&gt; file and rerun the script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;mv &lt;/span&gt;test.txt test.txt.backup
&lt;span class="nv"&gt;$ &lt;/span&gt;node script.js
&lt;span class="o"&gt;[&lt;/span&gt;Error: ENOENT: no such file or directory, open &lt;span class="s1"&gt;'./test.txt'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  errno: &lt;span class="nt"&gt;-2&lt;/span&gt;,
  code: &lt;span class="s1"&gt;'ENOENT'&lt;/span&gt;,
  syscall: &lt;span class="s1"&gt;'open'&lt;/span&gt;,
  path: &lt;span class="s1"&gt;'./test.txt'&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;Error: ENOENT: no such file or directory, open &lt;span class="s1"&gt;'./test.txt'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  errno: &lt;span class="nt"&gt;-2&lt;/span&gt;,
  code: &lt;span class="s1"&gt;'ENOENT'&lt;/span&gt;,
  syscall: &lt;span class="s1"&gt;'open'&lt;/span&gt;,
  path: &lt;span class="s1"&gt;'./test.txt'&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;Error: ENOENT: no such file or directory, open &lt;span class="s1"&gt;'./test.txt'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  errno: &lt;span class="nt"&gt;-2&lt;/span&gt;,
  code: &lt;span class="s1"&gt;'ENOENT'&lt;/span&gt;,
  syscall: &lt;span class="s1"&gt;'open'&lt;/span&gt;,
  path: &lt;span class="s1"&gt;'./test.txt'&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Showing that the error handling code, which is to just print the error to the console, works as expected in the 3 implementations.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>asyncawait</category>
      <category>promises</category>
      <category>callbacks</category>
    </item>
    <item>
      <title>How to apply type annotations to functions in TypeScript</title>
      <dc:creator>dade!</dc:creator>
      <pubDate>Sun, 21 Feb 2021 07:04:11 +0000</pubDate>
      <link>https://dev.to/dadepo/how-to-apply-type-annotations-to-functions-in-typescript-36n5</link>
      <guid>https://dev.to/dadepo/how-to-apply-type-annotations-to-functions-in-typescript-36n5</guid>
      <description>&lt;p&gt;This post explores the various ways of ascribing functions with type annotations in TypeScript. To fully appreciate the various ways of ascribing type annotations to functions, we would first take a quick look at the various ways functions can be defined in JavaScript. Once that is done, we then look at how to bring TypeScript's static type annotation into the picture. &lt;/p&gt;

&lt;p&gt;There are mainly two ways of defining functions in JavaScript. Namely: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Function expressions &lt;/li&gt;
&lt;li&gt;  Function declarations. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s quickly go over how these two work.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function expressions
&lt;/h3&gt;

&lt;p&gt;JavaScript allows creating functions by defining them as expressions that can be assigned to a variable. Defining such function expressions can make use of the &lt;code&gt;function&lt;/code&gt; keyword or via the use of Arrow functions. &lt;/p&gt;

&lt;p&gt;For example, defining an &lt;code&gt;add&lt;/code&gt; function can take either of these forms:&lt;/p&gt;

&lt;p&gt;Using the &lt;code&gt;function&lt;/code&gt; keyword:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using Arrow functions, It takes the form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Functions defined in either of these ways can be called like any other function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// logs 3 &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Function declaration
&lt;/h3&gt;

&lt;p&gt;Using function declaration is the regular way of defining a function that everyone knows.  &lt;/p&gt;

&lt;p&gt;It looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above is a declaration, &lt;em&gt;function declaration&lt;/em&gt; that creates an identifier, in this case, &lt;code&gt;add&lt;/code&gt;, that can be later used as a function. The created &lt;code&gt;add&lt;/code&gt; identifier can be called as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// logs 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now before we go ahead to see how to apply TypeScript’s type annotations, another important thing to be aware of, is the fact that &lt;strong&gt;functions in JavaScript are also objects!&lt;/strong&gt;  &lt;/p&gt;

&lt;p&gt;Yes, a function in JavaScript is a JavaScript object.  &lt;/p&gt;

&lt;p&gt;This means the function &lt;code&gt;add&lt;/code&gt; created above, either via function declaration or function expression, is also an object and can have properties assigned to it like any other object in JavaScript.  &lt;/p&gt;

&lt;p&gt;That is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serial&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;A01&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serial&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// logs A01&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a more in-depth take on functions being objects, see &lt;a href="https://www.geekabyte.io/2013/03/understanding-constructor-function-and.html"&gt;Understanding Constructor Function and this Keyword in Javascript&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Knowing that functions are also objects is important, as this also influences the syntax that can be used for type annotation in TypeScript. We will see how later in the post. &lt;/p&gt;

&lt;p&gt;Now that we have covered the basics, let’s now go into applying type annotations to functions. &lt;/p&gt;

&lt;p&gt;We will start with function expressions.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Adding Type Annotations to Function Expressions
&lt;/h3&gt;

&lt;p&gt;Adding type annotations to function expressions is straight forward because the definition is an expression that is assigned to a variable. &lt;/p&gt;

&lt;p&gt;This makes it obvious where to place the type annotation, which is right after the variable declaration.&lt;/p&gt;

&lt;p&gt;Knowing this, adding type annotation to the &lt;code&gt;add&lt;/code&gt; function will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that the actual function definition remains unchained. That is &lt;em&gt;(a,b) =&amp;gt; { return a + b }&lt;/em&gt; remains the way it was in the JavaScript version, and no type annotations are added to the function parameters, but TypeScript is able to infer the types based on type annotation ascribed to the variable. &lt;/p&gt;

&lt;p&gt;That being said, it is also possible to update the function definition to have type annotations. That is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
         &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In which case, the type annotation placed after the variable becomes redundant and can be removed, which leads to another way of typing the function expressions. This can be seen below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;This demonstrates an important point. Which is, when typing functions in TypeScript, there are two ways to go about it. One way is to ascribe type annotations to the function parameters and return type. The other way is to ascribe annotation to the variable that holds the function&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Ascribing type annotations to variables is possible when using function expression since function expression are assigned to variables. Function expression can also choose to have parameters in their definition annotated, although often time this is not needed.&lt;/p&gt;

&lt;p&gt;Another important point is when ascribing types to variable holding functions, the syntax used resembles how Arrow functions are used, that is it makes use of "&lt;strong&gt;=&amp;gt;&lt;/strong&gt;". And this is the only way to annotate variables with functions.  &lt;/p&gt;

&lt;p&gt;For example, this is correct:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
         &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While this leads to a syntax error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
             &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Adding Type Annotations to Function Declaration
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;add&lt;/code&gt; JavaScript function defined via function declaration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With TypeScripts type annotations applied becomes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since there is no other way the TypeScript compiler can infer the types of the function parameters, the type annotation has to be supplied.&lt;/p&gt;

&lt;p&gt;One might ask, what then is the type of the add function?&lt;/p&gt;

&lt;p&gt;For example given a variable name defined as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;John&lt;/span&gt; &lt;span class="nx"&gt;Doe&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the question is asked, what is the type of &lt;code&gt;name&lt;/code&gt; it is easy to see it is &lt;code&gt;string&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;When the same question is asked for the &lt;code&gt;add&lt;/code&gt; function defined using function expressions, that is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
         &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is easy to respond that the type is &lt;code&gt;(a: number, b: number) =&amp;gt; number&lt;/code&gt;  &lt;/p&gt;

&lt;p&gt;But what about the &lt;code&gt;add&lt;/code&gt; function defined using function declaration?&lt;/p&gt;

&lt;p&gt;To help answer this question we can use the combination of an IDE and the &lt;code&gt;typeof&lt;/code&gt; operator of TypeScript. The &lt;code&gt;typeof&lt;/code&gt; operator when used in the type signature context can help extract the type of a value. &lt;/p&gt;

&lt;p&gt;So to answer the question, what is the type of the add function defined using function declaration, we use &lt;code&gt;typeof&lt;/code&gt; on add, in the type signature context and using a tool that offers &lt;em&gt;IntelliSense&lt;/em&gt;, in this case, the TypeScript playground, we can see what the type is: &lt;/p&gt;

&lt;p&gt;&lt;span id="docs-internal-guid-acfc7784-7fff-4f7c-d71e-81412c140a0d"&gt;&lt;span&gt;&lt;span&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--J9wL3Z5r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/_vxiOiLSQb8bLs-7FrB4-7OE1eW_Nm4qP0pUPDplJrtxlkrOI1LObS9sh7kWelnrgG86qsjA6ya2-M4QtVOql032potmxX2vtHhUl-aVhY8Z1WXHBQOZe7X-3z1N8QZwMK-Tp1fw" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--J9wL3Z5r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/_vxiOiLSQb8bLs-7FrB4-7OE1eW_Nm4qP0pUPDplJrtxlkrOI1LObS9sh7kWelnrgG86qsjA6ya2-M4QtVOql032potmxX2vtHhUl-aVhY8Z1WXHBQOZe7X-3z1N8QZwMK-Tp1fw" alt=""&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;And as can be seen above, the type of &lt;code&gt;add&lt;/code&gt; when defined using function declaration is &lt;code&gt;(a: number, b: number) =&amp;gt; number&lt;/code&gt;, which is exactly the same type annotation of the same function when defined using the function expression!&lt;/p&gt;

&lt;h3&gt;
  
  
  Typing functions using Call signature of an object literal type
&lt;/h3&gt;

&lt;p&gt;Remember we mentioned that functions are also objects. And we showed how we can add properties to functions as we do to objects. Well, functions being objects also provide us another way of supplying type information about functions. &lt;/p&gt;

&lt;p&gt;A question a curious reader might ask upon being told that functions are objects in JavaScript is this: if functions are objects, how come we can call them? How come functions can be called by appending &lt;em&gt;()&lt;/em&gt; to the end of the function? That is something like &lt;code&gt;functionName()&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The answer to that question is in realising that the syntax &lt;code&gt;functionName()&lt;/code&gt; is really a syntactic sugar for either &lt;code&gt;functionName.call()&lt;/code&gt; or &lt;code&gt;functionName.apply()&lt;/code&gt;. That is, calling a function, is really nothing but assessing the &lt;code&gt;apply&lt;/code&gt; or &lt;code&gt;call&lt;/code&gt; property of the object representing that function. &lt;/p&gt;

&lt;p&gt;See MDN entries for &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply"&gt;Function.prototype.apply()&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call"&gt;Function.prototype.call()&lt;/a&gt; for more information. &lt;/p&gt;

&lt;p&gt;This knowledge helps in understanding another way of typing functions, which is using the call signature. Doing that builds on how object literal can be used to specify types. &lt;/p&gt;

&lt;p&gt;For example to provide type annotation describing an object with a property &lt;code&gt;name&lt;/code&gt; of type string, and property &lt;code&gt;age&lt;/code&gt;, of type number, the following interface can be created and used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
  &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;
  &lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;john&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John Doe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;hello&lt;/span&gt; &lt;span class="nx"&gt;world&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The type annotation outlines the property name together with a type annotation.  &lt;/p&gt;

&lt;p&gt;Knowing this, and knowing that functions are also objects, that can be called via a &lt;code&gt;call&lt;/code&gt;, or &lt;code&gt;apply&lt;/code&gt; property then we can provide a type annotation to our &lt;code&gt;add&lt;/code&gt; function as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Adder&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;
  &lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we can then use this &lt;code&gt;Adder&lt;/code&gt; in typing the &lt;code&gt;add&lt;/code&gt; function. This looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Adder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that even though this type checks, confirming that the created &lt;code&gt;Adder&lt;/code&gt; type can be used to annotate our &lt;code&gt;add&lt;/code&gt; function, we still have to annotate the type parameters, because TypeScript is unable to infer their types from the provided &lt;code&gt;Adder&lt;/code&gt; annotation used for the variable. &lt;/p&gt;

&lt;p&gt;We can go one step further in the definition of &lt;code&gt;Adder&lt;/code&gt; by removing the need to specify &lt;code&gt;apply&lt;/code&gt; and &lt;code&gt;call&lt;/code&gt; explicitly. This is because as we already know that a function (which is an object) can have its &lt;code&gt;apply&lt;/code&gt; and &lt;code&gt;call&lt;/code&gt; property called without having to explicitly specify them. That is, the call signature of a function is a syntactic sugar that will expand to explicitly use either &lt;code&gt;apply&lt;/code&gt; or &lt;code&gt;call&lt;/code&gt;. We can apply this knowledge of the call signature to the type definition by removing the &lt;code&gt;apply&lt;/code&gt; and &lt;code&gt;call&lt;/code&gt;. Doing that we end up with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Adder&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way of providing type annotations to functions is usually referred to as using the call signature of an object literal type.&lt;/p&gt;

&lt;p&gt;It is worth noting that in TypeScript, the keyword &lt;code&gt;type&lt;/code&gt; and &lt;code&gt;interface&lt;/code&gt; are interchangeable in most cases hence, the above can also be defined using &lt;code&gt;type&lt;/code&gt; instead of &lt;code&gt;interface&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;These conclude the overview of how functions can be typed in TypeScript. A quick outline of the key points is listed below:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;span&gt;The way functions are typed in TypeScript depends on the ways functions can be created in JavaScript.&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt; &lt;span&gt;Functions can be created either via function declaration or function expressions. &lt;/span&gt;
&lt;/li&gt;
&lt;li&gt; &lt;span&gt;They are two main ways to ascribe type annotations to functions. T*&lt;em&gt;yping the parameters and return type of the function&lt;/em&gt;&lt;em&gt;, or **typing the variable that holds the function&lt;/em&gt;*. &lt;/span&gt;
&lt;/li&gt;
&lt;li&gt; &lt;span&gt;Functions defined using function declaration can only be typed by providing type annotation to parameters and return value. A function expression can be typed by providing the type annotation to the variable that holds the function expression. Also, it is possible to ascribe type to the function parameters defined in the function expression, this is usually redundant. It is only needed in cases where the compiler cannot infer their types based on the type annotation ascribed to the variable.&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt; &lt;span&gt;When typing a variable that holds a function, the type annotation makes use of &lt;em&gt;=&amp;gt;&lt;/em&gt; to specify the return type. Using this Arrow function style is the only way to type variables holding function expressions.&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt; &lt;span&gt;Also, Functions are just objects! And this influences the third way of typing functions which is called: &lt;em&gt;call signature&lt;/em&gt; using object literals.&lt;/span&gt;
&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
