Categories
TypeScript Answers

How to declare abstract method in TypeScript?

To declare abstract method in TypeScript, we can use the abstract keyword.

For instance, we write

abstract class Animal {
  constructor(protected name: string) {}
  abstract makeSound(input: string): string;

  move(meters) {
    console.log(this.name, " moved");
  }
}

class Snake extends Animal {
  constructor(name: string) {
    super(name);
  }

  makeSound(input: string): string {
    return input;
  }

  move() {
    console.log("Slithering...");
    super.move(5);
  }
}

to create the Animal abstract class which have some methods that should be implemented when we create a class from it.

Then we create the Snake class that extends the Animal abstract class.

We implement all the methods in the abstract class in the class.

And we use super to reference the abstract class.

Categories
TypeScript Answers

How to return an empty Observable with Rxjs?

Sometimes, we want to return an empty Observable with Rxjs.

In this article, we’ll look at how to return an empty Observable with Rxjs.

How to return an empty Observable with Rxjs?

To return an empty Observable with Rxjs, we can use EMPTY or of.

For instance, we write

import { EMPTY, empty, of } from "rxjs";

EMPTY;
of({});

to import EMPTY which is an empty observable.

We can also call of with an empty object to return an empty observable.

Conclusion

To return an empty Observable with Rxjs, we can use EMPTY or of.

Categories
TypeScript Answers

How to make TypeScript interface properties optional?

Sometimes, we want to make TypeScript interface properties optional.

In this article, we’ll look at how to make TypeScript interface properties optional.

How to make TypeScript interface properties optional?

To make TypeScript interface properties optional, we can add a ? after the property name.

For instance, we write

interface I {
  a: string;
  b?: any;
  c?: AnotherType;
}

to create the I interface with the b and c properties made optional by appending a ? to each property name.

Conclusion

To make TypeScript interface properties optional, we can add a ? after the property name.

Categories
TypeScript Answers

How to cast a number to a string in TypeScript?

Sometimes, we want to cast a number to a string in TypeScript.

In this article, we’ll look at how to cast a number to a string in TypeScript.

How to cast a number to a string in TypeScript?

To cast a number to a string in TypeScript,. we can use the toString method.

For instance, we write

const pageNumber: number = 3;
const s: string = pageNumber.toString();

to call pageNumber.toString to return the number converted into a number string and then assign the string to s.

Conclusion

To cast a number to a string in TypeScript,. we can use the toString method.

Categories
TypeScript Answers

How to create an empty typed container array with TypeScript?

Sometimes, we want to create an empty typed container array with TypeScript.

In this article, we’ll look at how to create an empty typed container array with TypeScript.

How to create an empty typed container array with TypeScript?

To create an empty typed container array with TypeScript, we can create the variable and set it to the array type or we can cast an existing to the array type.

For instance, we write

const arr: Fruit[] = [];

to create the arr array to a Fruit array type.

We can also cast an array to the type we want by writing

const arr = <Fruit[]>[];

Conclusion

To create an empty typed container array with TypeScript, we can create the variable and set it to the array type or we can cast an existing to the array type.