Categories
TypeScript Answers

How to call an overridden method in base class constructor in TypeScript?

To call an overridden method in base class constructor in TypeScript, we can call the method from the base class’ instance.

For instance, we write

class A {
  protected doStuff() {
    console.log("Called from A");
  }

  public callDoStuff() {
    this.doStuff();
  }
}

class B extends A {
  protected doStuff() {
    console.log.doStuff();
    alert("Called from B");
  }
}

const a = new A();
a.callDoStuff();

const b = new B();
b.callDoStuff();

to call callDoStuff from an A instance a, which will log 'Called from A'.

If we call callDoStuff from a the B instance b, then we see 'Called from B' logged.

Conclusion

To call an overridden method in base class constructor in TypeScript, we can call the method from the base class’ instance.

Categories
TypeScript Answers

How to fix the “Type ‘null’ is not assignable to type ‘T'” error in TypeScript?

Sometimes, we want to fix the "Type ‘null’ is not assignable to type ‘T’" error in TypeScript.

In this article, we’ll look at how to fix the "Type ‘null’ is not assignable to type ‘T’" error in TypeScript.

How to fix the "Type ‘null’ is not assignable to type ‘T’" error in TypeScript?

To fix the "Type ‘null’ is not assignable to type ‘T’" error in TypeScript, we call add null as one of the return types of our function.

For instance, we write

class Foo {
  public static bar<T>(x: T): T | null {
    //...
    if (x === null) {
      return null;
    }
    //...
  }
}

to add the null type of as one of the data types that can be returned by the bar method in addition to the generic T type.

Conclusion

To fix the "Type ‘null’ is not assignable to type ‘T’" error in TypeScript, we call add null as one of the return types of our function.

Categories
TypeScript Answers

How to use object literal as TypeScript enum values?

Sometimes, we want to use object literal as TypeScript enum values.

In this article, we’ll look at how to use object literal as TypeScript enum values.

How to use object literal as TypeScript enum values?

To use object literal as TypeScript enum values, we can use the enum keys to create a new object set to objects.

For instance, we write

interface PizzaInfo {
  name: string;
  cost: number;
}

enum PizzaSize {
  SMALL,
  MEDIUM,
  LARGE,
}

const pizzas: Record<PizzaSize, PizzaInfo> = {
  [PizzaSize.SMALL]: { name: "Small", cost: 100 },
  [PizzaSize.MEDIUM]: { name: "Medium", cost: 200 },
  [PizzaSize.LARGE]: { name: "Large", cost: 300 },
};

to create the pizzas object that’s set to the Record type.

The Record type lets us specify the types of keys and values of an object.

We specified that the PizzaSize enum is the type of the keys and PizzaInfo is the type for the values.

Then we can use the enum value as the keys and objects as their values.

Conclusion

To use object literal as TypeScript enum values, we can use the enum keys to create a new object set to objects.

Categories
TypeScript Answers

How to add a private setter with TypeScript?

Sometimes, we want to add a private setter with TypeScript.

In this article, we’ll look at how to add a private setter with TypeScript.

How to add a private setter with TypeScript?

To add a private setter with TypeScript, we can use the private keyword.

For instance, we write

class Test {
  _prop: string;
  public get prop(): string {
    return this._prop;
  }

  private get internalProp(): string {
    return this.prop;
  }

  private set internalProp(value: string) {
    this._prop = value;
  }

  private addToProp(valueToAdd: string): void {
    this.internalProp += valueToAdd;
  }
}

to create the internalProp setter that sets the this._prop value to value.

Then we can use the setter as we do in the addToProp by assigning a value to this.internalProp.

Conclusion

To add a private setter with TypeScript, we can use the private keyword.

Categories
TypeScript Answers

How to initialize a multidimensional array with TypeScript?

Sometimes, we want to initialize a multidimensional array with TypeScript.

In this article, we’ll look at how to initialize a multidimensional array with TypeScript.

How to initialize a multidimensional array with TypeScript?

To initialize a multidimensional array with TypeScript, we can use the array fill and map methods.

For instance, we write

const n = 10;
const palindrome: boolean[][] = new Array(n)
  .fill(false)
  .map(() => new Array(n).fill(false));

to create a 10×10 palindrome array that’s filled with false everywhere.

We do that by using Array with n to create an array with length 10.

And then we call fill to fill that with false.

Then we call map with a callback that map the entries in the array to arrays with 10 false entries.

Conclusion

To initialize a multidimensional array with TypeScript, we can use the array fill and map methods.