Categories
TypeScript Answers

How to use RegExp in TypeScript?

Sometimes, we want to use RegExp in TypeScript.

In this article, we’ll look at how to use RegExp in TypeScript.

How to use RegExp in TypeScript?

To use RegExp in TypeScript, we can use it as we do with JavaScript.

For instance, we write

const trigger = "2";
const regexp = new RegExp("^[1-9]d{0,2}$");
const test = regexp.test(trigger);

to create a new regex with the RegExp constructor that matches any number with 0 to 2 digits.

Then we call test with the trigger string to see if it matches.

Conclusion

To use RegExp in TypeScript, we can use it as we do with JavaScript.

Categories
TypeScript Answers

How to fix the “Argument of type ‘X’ is not assignable to parameter of type ‘X'” error with TypeScript?

Sometimes, we want to fix the "Argument of type ‘X’ is not assignable to parameter of type ‘X’" error with TypeScript.

In this article, we’ll look at how to fix the "Argument of type ‘X’ is not assignable to parameter of type ‘X’" error with TypeScript.

How to fix the "Argument of type ‘X’ is not assignable to parameter of type ‘X’" error with TypeScript?

To fix the "Argument of type ‘X’ is not assignable to parameter of type ‘X’" error with TypeScript, we should restart VS Code to clear the error.

We should also make sure our code has no syntax errors.

Once we restart VS Code and the syntax errors are removed, the error should clear.

Conclusion

To fix the "Argument of type ‘X’ is not assignable to parameter of type ‘X’" error with TypeScript, we should restart VS Code to clear the error.

We should also make sure our code has no syntax errors.

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.