<?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: amr</title>
    <description>The latest articles on DEV Community by amr (@amrwaheed).</description>
    <link>https://dev.to/amrwaheed</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F476315%2F584a8483-e88a-438a-8ab4-20c58a9db22a.jpeg</url>
      <title>DEV Community: amr</title>
      <link>https://dev.to/amrwaheed</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amrwaheed"/>
    <language>en</language>
    <item>
      <title>"An Introduction to Types in TypeScript: Built-In Types, Type Annotations, Interfaces, and Classes"</title>
      <dc:creator>amr</dc:creator>
      <pubDate>Tue, 28 Mar 2023 02:12:35 +0000</pubDate>
      <link>https://dev.to/amrwaheed/types-in-typescript-1jf5</link>
      <guid>https://dev.to/amrwaheed/types-in-typescript-1jf5</guid>
      <description>&lt;p&gt;TypeScript is a statically typed superset of JavaScript that adds optional type annotations to the language. In TypeScript, you can specify types for variables, function parameters, function return types, and object properties. TypeScript provides a number of built-in types, as well as the ability to create custom types using interfaces and classes.&lt;/p&gt;

&lt;p&gt;Here are some of the built-in types in TypeScript:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;number&lt;/code&gt;: represents numeric values, including integers and floating-point numbers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;string&lt;/code&gt;: represents string values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;boolean&lt;/code&gt;: represents boolean values (true/false).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;null&lt;/code&gt;: represents the absence of a value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;undefined&lt;/code&gt;: represents a value that has not been defined.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;void&lt;/code&gt;: represents the absence of a return value from a function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;any&lt;/code&gt;: represents any type of value, and can be used to opt out of &lt;br&gt;
type checking for a specific value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;never&lt;/code&gt;: represents a value that will never occur, such as a function that always throws an error.&lt;br&gt;
To specify a type for a variable, function parameter, or function return type, you can use a colon followed by the type. For example:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let myNumber: number = 42;
let myString: string = 'hello';
let myBoolean: boolean = true;

function addNumbers(a: number, b: number): number {
  return a + b;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, we've used type annotations to specify the types of the &lt;code&gt;myNumber&lt;/code&gt;, &lt;code&gt;myString&lt;/code&gt;, and &lt;code&gt;myBoolean&lt;/code&gt; variables, as well as the &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt;, and return types of the &lt;code&gt;addNumbers&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;You can also create custom types in TypeScript using interfaces and classes. Interfaces define a shape for an object, specifying the names and types of its properties. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Person {
  firstName: string;
  lastName: string;
  age: number;
}

let person: Person = {
  firstName: 'John',
  lastName: 'Doe',
  age: 42,
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, we've defined an interface &lt;code&gt;Person&lt;/code&gt; with three properties: &lt;code&gt;firstName&lt;/code&gt;, &lt;code&gt;lastName&lt;/code&gt;, and &lt;code&gt;age&lt;/code&gt;. We've then created an object that conforms to this interface and assigned it to the &lt;code&gt;person&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;Classes in TypeScript are similar to classes in other object-oriented languages, and can include properties and methods with specified types. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Animal {
  name: string;

  constructor(name: string) {
    this.name = name;
  }

  speak(): void {
    console.log(`${this.name} makes a noise.`);
  }
}

class Dog extends Animal {
  breed: string;

  constructor(name: string, breed: string) {
    super(name);
    this.breed = breed;
  }

  speak(): void {
    console.log(`${this.name} barks.`);
  }
}

let myDog: Dog = new Dog('Fido', 'Labrador');
myDog.speak(); // output: "Fido barks."

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, we've defined a class &lt;code&gt;Animal&lt;/code&gt; with a &lt;code&gt;name&lt;/code&gt; property and a &lt;code&gt;speak&lt;/code&gt; method. We've then defined a &lt;code&gt;Dog&lt;/code&gt; class that extends &lt;code&gt;Animal&lt;/code&gt; and adds a &lt;code&gt;breed&lt;/code&gt; property and a custom &lt;code&gt;speak&lt;/code&gt; method. We've created a new instance of &lt;code&gt;Dog&lt;/code&gt; and assigned it to the &lt;code&gt;myDog&lt;/code&gt; variable, and called the &lt;code&gt;speak&lt;/code&gt; method on it.&lt;/p&gt;

&lt;p&gt;I hope this gives you a good introduction to types in TypeScript.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
