Categories
TypeScript Answers

How to reexport a class in TypeScript?

Sometimes, we want to reexport a class in TypeScript.

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

How to reexport a class in TypeScript?

To reexport a class in TypeScript, we can use the export keyword.

To export a class that’s a member of another module, we write

export { A } from "a";

where A is the class that we want to export from module a.

To export a class that’s a default export, we write

export { default } from "d";

to export default instead of the class name.

Conclusion

To reexport a class in TypeScript, we can use the export keyword.

Categories
TypeScript Answers

How to fix the “is not newable” error when passing class as parameter with TypeScript?

Sometimes, we want to fix the "is not newable" error when passing class as parameter with TypeScript.

In this article, we’ll look at how to fix the "is not newable" error when passing class as parameter with TypeScript.

How to fix the "is not newable" error when passing class as parameter with TypeScript?

To fix the "is not newable" error when passing class as parameter with TypeScript, we can create a interface with the new keyword and then use that as the type of the class parameter.

For instance, we write

interface IMyClass {
  new (name: string): MyClass;
}

const test = (MyClass: IMyClass) => {
  const obj = new MyClass("hello");
};

to create the IMyClass interface that has the new property with the signature being the MyClas constructor’s signature and the type set to MyClass.

Then we set that as the type of the MyClass parameter in the test function and then we can create a new MyClass instance in the function without errors.

Conclusion

To fix the "is not newable" error when passing class as parameter with TypeScript, we can create a interface with the new keyword and then use that as the type of the class parameter.

Categories
TypeScript Answers

How to check for string equality with TypeScript?

Sometimes, we want to check for string equality with TypeScript.

In this article, we’ll look at how to check for string equality with TypeScript.

How to check for string equality with TypeScript?

To check for string equality with TypeScript, we can use the === operator.

For instance, we write

if (x === y) {
} else {
}

to check if x and y have the same value with ===.

Conclusion

To check for string equality with TypeScript, we can use the === operator.

Categories
TypeScript Answers

How to fix the error “TS2339: Property ‘x’ does not exist on type ‘Y'” with TypeScript?

Sometimes, we want to fix the error "TS2339: Property ‘x’ does not exist on type ‘Y’" with TypeScript.

In this article, we’ll look at how to fix the error "TS2339: Property ‘x’ does not exist on type ‘Y’" with TypeScript.

How to fix the error "TS2339: Property ‘x’ does not exist on type ‘Y’" with TypeScript?

To fix the error "TS2339: Property ‘x’ does not exist on type ‘Y’" with TypeScript, we should make sure the properties are listed in the interface that’s set as the type of the object.

For instance, we write

interface Images {
  main: string;
  [key: string]: string;
}

const getMainImageUrl = (images: Images): string => {
  return images.main;
};

to create the Images interface that has the main property.

Then we can access the images.main property in the getMainImageUrl function without TypeScript compiler errors.

Conclusion

To fix the error "TS2339: Property ‘x’ does not exist on type ‘Y’" with TypeScript, we should make sure the properties are listed in the interface that’s set as the type of the object.

Categories
TypeScript Answers

How to get properties of a class with TypeScript?

Sometimes, we want to get properties of a class with TypeScript.

In this article, we’ll look at how to get properties of a class with TypeScript.

How to get properties of a class with TypeScript?

To get properties of a class with TypeScript, we can create an instance of it and then use Object.getOwnPropertyNames on the instance.

For instance, we write

class A {
  private a1 = "";
  public a2 = "";
}

const a = new A();
const array = Object.getOwnPropertyNames(a);

to create an A instance.

Then we call Object.getOwnPropertyNames with A instance a to get all the non-inherited properties’ names’ in the array.

Conclusion

To get properties of a class with TypeScript, we can create an instance of it and then use Object.getOwnPropertyNames on the instance.