Categories
TypeScript Answers

How to iterate over array of objects in TypeScript?

Sometimes, we want to iterate over array of objects in TypeScript.

In this article, we’ll look at how to iterate over array of objects in TypeScript.

How to iterate over array of objects in TypeScript?

To iterate over array of objects in TypeScript, we can use a for-of loop.

For instance, we write

for (const product of products) {
  console.log(product.productDesc);
}

to loop through the products array with a for-of loop.

In it, we log the productDesc property of the products object entry being looped through, which is stored in product.

Conclusion

To iterate over array of objects in TypeScript, we can use a for-of loop.

Categories
TypeScript Answers

How to use Jasmine’s spyOn upon a private method with TypeScript?

Sometimes, we want to use Jasmine’s spyOn upon a private method with TypeScript.

In this article, we’ll look at how to use Jasmine’s spyOn upon a private method with TypeScript.

How to use Jasmine’s spyOn upon a private method with TypeScript?

To use Jasmine’s spyOn upon a private method with TypeScript, we can put the any type as the type parameter for spyOn.

For instance, in our test callback, we write

spyOn<any>(fakePerson, "sayHello");
expect(fakePerson["sayHello"]).toHaveBeenCalled();

to call spyOn with the <any> type parameter to spy on the fakePerson.sayHello method.

Then we call expect with fakePerson["sayHello"] to check that fakePerson.sayHello has been called with toHaveBeenCalled.

Conclusion

To use Jasmine’s spyOn upon a private method with TypeScript, we can put the any type as the type parameter for spyOn.

Categories
TypeScript Answers

How to inject Nest.js service from another module with TypeScript?

Sometimes, we want to inject Nest.js service from another module with TypeScript.

In this article, we’ll look at how to inject Nest.js service from another module with TypeScript.

How to inject Nest.js service from another module with TypeScript?

To inject Nest.js service from another module with TypeScript, we’ve to export the module before we can import it.

For instance, we write

@Module({
  controllers: [ItemsController],
  providers: [ItemsService],
  exports: [ItemsService],
})
export class ItemsModule {}

to export the ItemsService with

exports: [ItemsService],

Then in another module file, we write

@Module({
  controllers: [FooController],
  providers: [FooService],
  imports: [ItemsModule],
})
export class FooModule {}

to import the ItemsModule with

imports: [ItemsModule],

Conclusion

To inject Nest.js service from another module with TypeScript, we’ve to export the module before we can import it.

Categories
TypeScript Answers

How to watch and compile all TypeScript sources?

Sometimes, we want to watch and compile all TypeScript sources.

In this article, we’ll look at how to watch and compile all TypeScript sources.

How to watch and compile all TypeScript sources?

To watch and compile all TypeScript sources, we can make some changes to the tsconfig.json file in our TypeScript project.

For instance, we write

{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "module": "commonjs",
    "target": "ES5",
    "outDir": "ts-built",
    "rootDir": "src"
  }
}

to set the rootDir property to the path of the folder with all the files we want to compile.

Conclusion

To watch and compile all TypeScript sources, we can make some changes to the tsconfig.json file in our TypeScript project.

Categories
TypeScript Answers

How to check if an object implements an interface at runtime with TypeScript?

Sometimes, we want to check if an object implements an interface at runtime with TypeScript.

In this article, we’ll look at how to check if an object implements an interface at runtime with TypeScript.

How to check if an object implements an interface at runtime with TypeScript?

To check if an object implements an interface at runtime with TypeScript, we can create our own function.

For instance, we write

interface Test {
  prop: number;
}

function isTest(arg: any): arg is Test {
  return typeof arg?.prop === "number";
}

to define the isTest function that returns the arg is Test type.

This lets the TypeScript compiler know that it’s a type guard function.

Then we check if arg.prop is a number with typeof in it and return the result.

Conclusion

To check if an object implements an interface at runtime with TypeScript, we can create our own function.