<?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: Avishka Guruge</title>
    <description>The latest articles on DEV Community by Avishka Guruge (@avishka98).</description>
    <link>https://dev.to/avishka98</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%2F1237571%2Fa0f9fb92-1aae-493a-8dbb-9fde3475ab24.jpg</url>
      <title>DEV Community: Avishka Guruge</title>
      <link>https://dev.to/avishka98</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/avishka98"/>
    <language>en</language>
    <item>
      <title>Using JavaScript Packages without TypeScript Declarations in TypeScript</title>
      <dc:creator>Avishka Guruge</dc:creator>
      <pubDate>Fri, 22 Dec 2023 09:55:50 +0000</pubDate>
      <link>https://dev.to/avishka98/using-javascript-packages-without-typescript-declarations-in-typescript-3mhg</link>
      <guid>https://dev.to/avishka98/using-javascript-packages-without-typescript-declarations-in-typescript-3mhg</guid>
      <description>&lt;p&gt;Let's assume you want to install and use a third-party package in your TypeScript project. When you try to import it into a file, you might encounter a &lt;code&gt;ts(7016)&lt;/code&gt; error.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7h5YoAF---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x87utr9tytbwicr1961p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7h5YoAF---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x87utr9tytbwicr1961p.png" alt="ts(7016) error" width="800" height="158"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Try installing its type declaration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i --save-dev @types/react-native-scroll-indicator
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even attempting to install &lt;code&gt;@types/react-native-scroll-indicator&lt;/code&gt; might result in a failure with a &lt;code&gt;Not found&lt;/code&gt; error.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This means that the package does not include its own Typescript definitions.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;But, TypeScript needs to know the types of the functions and variables in the package to ensure type safety.&lt;/p&gt;

&lt;p&gt;No need to worry! You can still incorporate this package into your project. Here's how.&lt;/p&gt;

&lt;p&gt;There are a few ways you can address this issue.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Method 1: Write your own type definitions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;write a &lt;code&gt;.d.ts&lt;/code&gt; file for the library in your project's working copy, as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;declare module 'some-library' {
  export const doInterestingThings(): void;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, it's worth noting that the definitions could be extensive. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Therefore, this approach is not recommended, given its potential for being time-consuming and error-prone.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Method 2: Generate type definitions with &lt;code&gt;dts-gen&lt;/code&gt; tool&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;dts-gen&lt;/code&gt; is a tool that generates TypeScript definition files (.d.ts) from any JavaScript object.&lt;/p&gt;

&lt;p&gt;It's incredibly simple!&lt;/p&gt;

&lt;p&gt;You'll need to first install &lt;code&gt;dts-gen&lt;/code&gt; if you haven't already:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -g dts-gen
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Navigate to your project directory. Now, Install your package if you haven't already installed.&lt;/p&gt;

&lt;p&gt;Run the following command to generate the type definitions for the &lt;code&gt;some-library&lt;/code&gt; package:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dts-gen -m some-library
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will generate a file named &lt;code&gt;some-library.d.ts&lt;/code&gt; in your project directory with the type definitions for the &lt;code&gt;some-library&lt;/code&gt; package.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In most cases, this method works well, unless there's an error in the JavaScript files.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;In such a scenario, you can do the following:&lt;/p&gt;

&lt;p&gt;write a &lt;code&gt;.d.ts&lt;/code&gt; file for the library in your project's working copy, as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;declare module 'some-library';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Essentially, it informs TypeScript, &lt;em&gt;"Hey, this thing exists; stop flagging errors, and let me use it as I see fit."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This means that you won’t get any type checking or autocompletion for those functions or variables.🥲&lt;/p&gt;




&lt;p&gt;Hence, &lt;strong&gt;Method 2&lt;/strong&gt; is typically the recommended approach, as it offers more comprehensive type information and helps minimize the risk of errors.&lt;/p&gt;

&lt;p&gt;I hope this helps! Let me know if you have any other questions.&lt;/p&gt;

&lt;p&gt;Good luck!&lt;/p&gt;

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