Categories
TypeScript Answers

How to add enum flags in TypeScript?

Sometimes, we want to add enum flags in TypeScript.

In this article, we’ll look at how to add enum flags in TypeScript.

How to add enum flags in TypeScript?

To add enum flags in TypeScript, we can assign the enum entries to binary values.

For instance, we write

enum Traits {
  None = 0,
  Friendly = 1 << 0,
  Mean = 1 << 1,
  Funny = 1 << 2,
  Boring = 1 << 3,
  All = ~(~0 << 4),
}

to assign each entry of Traits to binary values.

Then we can combine them with bitwise operators like

const traits = Traits.Mean | Traits.Funny;

Conclusion

To add enum flags in TypeScript, we can assign the enum entries to binary values.

Categories
TypeScript Answers

How to define an object of objects type in TypeScript?

Sometimes, we want to define an object of objects type in TypeScript.

In this article, we’ll look at how to define an object of objects type in TypeScript.

How to define an object of objects type in TypeScript?

To define an object of objects type in TypeScript, we can use index signatures with the type set to the type for the value object.

For instance, we write

const data: { [name: string]: DataModel } = {
  //...
};

to create a data variable of type { [name: string]: DataModel } where DataModel is an object type.

[name: string] is the index signature that lets us add any property with string keys.

As long as the structure of data matches the structure specified in the type, the type check will pass.

Conclusion

To define an object of objects type in TypeScript, we can use index signatures with the type set to the type for the value object.

Categories
TypeScript Answers

How to return a proper Promise with TypeScript?

Sometimes, we want to return a proper Promise with TypeScript.

In this article, we’ll look at how to return a proper Promise with TypeScript.

How to return a proper Promise with TypeScript?

To return a proper Promise with TypeScript, we can at the type for the resolved value of the promise.

For instance, we write

class C {
  //...
  saveMyClass(updatedMyClass: MyClass) {
    return new Promise<Package>((resolve, reject) => {
      const savedPackage: Package = updatedPackage;
      //...
      setTimeout(() => {
        resolve(savedPackage);
      }, 1500);
    });
  }
  //...
}

to return a promise in the saveMyClass method that has a Package object as the promise’s resolve value.

This is specified with <Package>.

We set savedPackage to the Package type and call resolve with it to make the code work with the type checks.

Conclusion

To return a proper Promise with TypeScript, we can at the type for the resolved value of the promise.

Categories
TypeScript Answers

How to declare a nested class structure in TypeScript?

Sometimes, we want to declare a nested class structure in TypeScript.

In this article, we’ll look at how to declare a nested class structure in TypeScript.

How to declare a nested class structure in TypeScript?

To declare a nested class structure in TypeScript, we can create a namespace or module and put our class in it.

For instance, we write

class Outer {}

namespace Outer {
  export class Mid {}

  export module Mid {
    export class Inner {}
  }
}

const a = new Outer();
const b = new Outer.Mid();
const x = new Outer.Mid.Inner();

to create the Outer namespace.

And we put the Mid class in it.

Then we create the Mid module in the same namespace and put the Inner class in it.

Then we can access the 3 classes as we did in the last 3 lines.

Anything before the class name is the namespace or module name.

Conclusion

To declare a nested class structure in TypeScript, we can create a namespace or module and put our class in it.

Categories
TypeScript Answers

How to fix the ‘Jest did not exit one second after the test run has completed’ error with TypeScript?

Sometimes, we want to fix the ‘Jest did not exit one second after the test run has completed’ error with TypeScript.

In this article, we’ll look at how to fix the ‘Jest did not exit one second after the test run has completed’ error with TypeScript.

How to fix the ‘Jest did not exit one second after the test run has completed’ error with TypeScript?

To fix the ‘Jest did not exit one second after the test run has completed’ error with TypeScript, we should make sure we clean up when our test is done.

For instance, we write

beforeAll((done) => {
  done();
});

//...

afterAll((done) => {
  mongoose.connection.close();
  done();
});

to call done after calling mongoose.connection.close(); to close the MongoDB connection after our test is done.

Then Jest knows that the test is done and ends the test.

Conclusion

To fix the ‘Jest did not exit one second after the test run has completed’ error with TypeScript, we should make sure we clean up when our test is done.