Basic Types - TypeScript

Basic Types - TypeScript

What is TypeScript?

TypeScript is a strongly typed, object oriented, compiled language. It was designed by Anders Hejlsberg (designer of C#) at Microsoft. TypeScript is both a language and a set of tools. TypeScript is a typed superset of JavaScript compiled to JavaScript. In other words, TypeScript is JavaScript plus some additional features.

1_UIioHehyD5o_6ydf3w2fuw.png

Basic Types

You can use the strong data types that TypeScript provides. Now I will explain the basic TypeScript data types.

Boolean

The most basic datatype is the simple true/false value, which JavaScript and TypeScript call a boolean value.

let isEmpty: boolean = false;

String

We use the type string to work with textual data. Represents a sequence of Unicode characters. Just like JavaScript, TypeScript also uses double quotes (") or single quotes (') to surround string data.

let fruit: string = "apple";
fruit= "apple";

Number

As in JavaScript, all numbers in TypeScript are either floating point values or BigIntegers:

  • These floating point numbers get the type number**.
  • while BigIntegers get the type bigint.
    let decimal: number = 6;
    let hex: number = 0xf00d;
    let binary: number = 0b1010;
    let octal: number = 0o744;
    let big: bigint = 100n;
    

Array

TypeScript like JavaScript, allows you to work with array of values. Array types can be written in one of two ways:

Use the type of the elements followed by [] to denote an array of that element type:

let list: number[] = [1, 2, 3];

Uses a generic array type, Array:

let list: Array<number> = [1, 2, 3];

Any

In some situations, not all type information is available or its declaration would take an inappropriate amount of effort. These may occur for values from code that has been written without TypeScript or a 3rd party library. In these cases, we might want to opt-out of type checking. To do so, we label these values with the any type:

declare function getValue(key: string): any;
// OK, return value of 'getValue' is not checked
const str: string = getValue("myString");

Object

object is a type that represents the non-primitive type, i.e. anything that is not number, string, boolean, bigint, symbol, null, or undefined.

With object type, APIs like Object.create can be better represented. For example:

declare function create(o: object | null): void;

// OK
create({ prop: 0 });
create(null);

create(42);
// [error]: Argument of type '42' is not assignable to parameter of type 'object | null'.
create("string");
// [error]:Argument of type '"string"' is not assignable to parameter of type 'object | null'.
create(false);
// [error]:Argument of type 'false' is not assignable to parameter of type 'object | null'.
create(undefined);
// [error]:Argument of type 'undefined' is not assignable to parameter of type 'object | null'

Void

void is a little like the opposite of any: the absence of having any type at all. You may commonly see this as the return type of functions that do not return a value:

function warnUser(): void {
  console.log("This is my warning message");
}

These are the most important data types, but there are more other useful data types that you can find in official typescript documentation

Thanks for reading, see you soon !!
Resource: