DEV Community

Fatima Alam
Fatima Alam

Posted on

TypeScript

Intro:

  1. Superset of JavaScript.
  2. Object-oriented and tightly typed programming language.
  3. TypeScript code is transformed to JavaScript, which may be used in any environment that supports JavaScript,
  4. This including browsers, Node.js, and your own applications.

Array in TypeScript

Using square brackets ([]):

let numbers: number[] = [1, 2, 3, 4, 5];
let strings: string[] = ['a', 'b', 'c'];
Enter fullscreen mode Exit fullscreen mode

Using the Array generic type:

let numbers: Array<number> = [1, 2, 3, 4, 5];
let strings: Array<string> = ['a', 'b', 'c'];
Enter fullscreen mode Exit fullscreen mode

Mixed array types:

let mixed: (number | string)[] = [1, 'two', 3, 'four'];
Enter fullscreen mode Exit fullscreen mode

Using a tuple (fixed length and types):

let tuple: [number, string, boolean] = [1, 'hello', true];
Enter fullscreen mode Exit fullscreen mode

Disadvantages of TypeScript

  1. It takes a long time to compile TypeScript code.
  2. Abstract classes are not supported in TypeScript.
  3. Converting TypeScript to JavaScript requires an additional compilation step.
  4. Its type scheme is extremely complicated.

never and any
Key Differences
never:

Used for functions that do not return.
Represents a value that never occurs.
Useful for exhaustive type checking.
any:

Disables type checking for a variable.
Can hold any type of value.
Useful for migrating JavaScript to TypeScript or working with dynamic data

Tuples

// define our tuple
let ourTuple: [number, boolean, string];

// initialized incorrectly which throws an error
ourTuple = [false, 'Coding God was mistaken', 5];
Enter fullscreen mode Exit fullscreen mode
// define our tuple
let ourTuple: [number, boolean, string];
// initialize correctly
ourTuple = [5, false, 'Coding God was here'];
// We have no type safety in our tuple for indexes 3+
ourTuple.push('Something new and wrong');
console.log(ourTuple);
Enter fullscreen mode Exit fullscreen mode

New valueTuples only have strongly defined types for the initial values:

// define our readonly tuple
const ourReadonlyTuple: readonly [number, boolean, string] = [5, true, 'The Real Coding God'];
// throws error as it is readonly.
ourReadonlyTuple.push('Coding God took a day off');
Enter fullscreen mode Exit fullscreen mode

Function

// the `: number` here specifies that this function returns a number
function getTime(): number {
  return new Date().getTime();
}
Enter fullscreen mode Exit fullscreen mode

Optional Parameter

// the `?` operator here marks parameter `c` as optional
function add(a: number, b: number, c?: number) {
  return a + b + (c || 0);
}
Enter fullscreen mode Exit fullscreen mode

Default Parameters

function pow(value: number, exponent: number = 10) {
  return value ** exponent;
}
Enter fullscreen mode Exit fullscreen mode

Object as an argument

function divide({ dividend, divisor }: { dividend: number, divisor: number }) {
  return dividend / divisor;
}
Enter fullscreen mode Exit fullscreen mode

Rest Paramter

function add(a: number, b: number, ...rest: number[]) {
  return a + b + rest.reduce((p, c) => p + c, 0);
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)