Categories
TypeScript Answers

How to extending error class with TypeScript?

To extending error class with TypeScript, we call Object.setPrototypeOf to set the prototype of our error class.

For instance, we write

class FooError extends Error {
  constructor(m: string) {
    super(m);
    Object.setPrototypeOf(this, FooError.prototype);
  }

  sayHello() {
    return "hello " + this.message;
  }
}

to create a FooError class that extends the Error class.

Then we call Object.setPrototypeOf to set the prototype of this to FooError.prototype in the constructor.

this then inherits from FooError.prototype.

Then we can use Error class methods without errors.

Conclusion

To extending error class with TypeScript, we call Object.setPrototypeOf to set the prototype of our error class.

Categories
TypeScript Answers

How to find module “fs” in VS Code with TypeScript?

Sometimes, we want to find module "fs" in VS Code with TypeScript.

In this article, we’ll look at how to find module "fs" in VS Code with TypeScript.

How to find module "fs" in VS Code with TypeScript?

To find module "fs" in VS Code with TypeScript, we need to install the @types/node package.

To install it, we run

npm install --save-dev @types/node

in our project folder.

This will add the type definitions for built in Node.js modules into our Node TypeScript project.

Conclusion

To find module "fs" in VS Code with TypeScript, we need to install the @types/node package.

Categories
TypeScript Answers

How to extend two classes with TypeScript?

Sometimes, we want to extend two classes with TypeScript.

In this article, we’ll look at how to extend two classes with TypeScript.

How to extend two classes with TypeScript?

To extend two classes with TypeScript, we merge all the properties in each class’ prototype into the class that we want to inherit from the two classes.

For instance, we write

class CanEat {
  public eat() {
    alert("eat.");
  }
}

class CanSleep {
  sleep() {
    alert("sleep.");
  }
}

const applyMixins = (derivedCtor: any, baseCtors: any[]) => {
  baseCtors.forEach((baseCtor) => {
    Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
      if (name !== "constructor") {
        derivedCtor.prototype[name] = baseCtor.prototype[name];
      }
    });
  });
};

class Being implements CanEat, CanSleep {
  eat: () => void;
  sleep: () => void;
}
applyMixins(Being, [CanEat, CanSleep]);

to define the applyMixins function.

In it, we loop through each class in baseCtors with forEach.

In the forEach callback, we get the property names of each class’ prototype with Object.getOwnPropertyNames.

And then we loop through each property name string with forEach.

If its name isn’t 'constructor'.

Then we assign baseCtor.prototype[name] to derivedCtor.prototype[name].

Next, we create the Being class that implements the CanEat and CanSleep classes.

And we call applyMixins to incorporate the property properties from the CanEat and CanSleep classes into the Being class’ prototype.

Conclusion

To extend two classes with TypeScript, we merge all the properties in each class’ prototype into the class that we want to inherit from the two classes.

Categories
TypeScript Answers

How to fix Property does not exist on type ‘object’ with TypeScript?

Sometimes, we want to fix Property does not exist on type ‘object’ with TypeScript.

In this article, we’ll look at how to fix Property does not exist on type ‘object’ with TypeScript.

How to fix Property does not exist on type ‘object’ with TypeScript?

To fix Property does not exist on type ‘object’ with TypeScript, we can add the missing property to the type that we set for the variable.

For instance, we write

interface Provider {
  region: string;
  country: string;
  locale: string;
  company: string;
}

let countryProviders: Array<Provider>;
let allProviders: Array<Provider>;

to create the Provider interface with the properties that we expect to have in an object with type Provider and their types.

And then we define variables that has an array type with entries in them being Provider objects.

Conclusion

To fix Property does not exist on type ‘object’ with TypeScript, we can add the missing property to the type that we set for the variable.

Categories
TypeScript Answers

How to format a date or time in TypeScript?

Sometimes, we want to format a date or time in TypeScript.

In this article, we’ll look at how to format a date or time in TypeScript.

How to format a date or time in TypeScript?

To format a date or time in TypeScript, we can use the date’s toLocaleString method.

For instance, we write

const s = new Date().toLocaleString();

to create a new Date object and then call toLocaleString on it to return a string that has the formatted datetime of the current time formatted according to the current locale.

Conclusion

To format a date or time in TypeScript, we can use the date’s toLocaleString method.