Categories
TypeScript Answers

How to fix the “property does not exist on ‘Object'” error in TypeScript?

Sometimes, we want to fix the "property does not exist on ‘Object’" error in TypeScript.

In this article, we’ll look at how to fix the "property does not exist on ‘Object’" error in TypeScript.

How to fix the "property does not exist on ‘Object’" error in TypeScript?

To fix the "property does not exist on ‘Object’" error in TypeScript, we can define an interface and use that as the type instead of using the Object type.

For instance, we write

interface Options {
  selector?: string;
  template?: string;
}

const doStuff = (o: Options) => {
  //...
};

doStuff({});
doStuff({ selector: "foo" });

to add the Options interface that has some optional properties inside.

Then we use that as the type for the o parameter in doStuff.

Finally, we can call it with some objects that matches the structure of the Options interface.

Conclusion

To fix the "property does not exist on ‘Object’" error in TypeScript, we can define an interface and use that as the type instead of using the Object type.

Categories
TypeScript Answers

How to fix sort by date not working with TypeScript?

Sometimes, we want to fix sort by date not working with TypeScript.

In this article, we’ll look at how to fix sort by date not working with TypeScript.

How to fix sort by date not working with TypeScript?

To fix sort by date not working with TypeScript, we can use the difference in the timestamps between 2 entries in the array.

For instance, we write

const sorted = myArray.sort((a: TaskItemVO, b: TaskItemVO) => {
  return a.dueDate.getTime() - b.dueDate.getTime();
});

to call myArray.sort with a callback that returns the difference of the dueDate timestamps which we get from the getTime method.

The sort callback should return a number and subtracting timestamps would return a number since timestamps are numbers.

Conclusion

To fix sort by date not working with TypeScript, we can use the difference in the timestamps between 2 entries in the array.

Categories
TypeScript Answers

How to merge two enums in TypeScript?

Sometimes, we want to merge two enums in TypeScript.

In this article, we’ll look at how to merge two enums in TypeScript.

How to merge two enums in TypeScript?

To merge two enums in TypeScript, we can use the spread operator.

For instance, we write

enum Mammals {
  Humans = "Humans",
  Bats = "Bats",
  Dolphins = "Dolphins",
}

enum Reptiles {
  Snakes = "Snakes",
  Alligators = "Alligators",
  Lizards = "Lizards",
}

const Animals = { ...Mammals, ...Reptiles };
type Animals = typeof Animals;

to create the Mammals and Reptiles enums.

Then we combine them in the Animals object by spreading the entries of Mammals and Reptiles into it.

And finally, we create the Animals type by using the typeof operator on Animals.

Conclusion

To merge two enums in TypeScript, we can use the spread operator.

Categories
TypeScript Answers

How to import all types with TypeScript?

Sometimes, we want to import all types with TypeScript.

In this article, we’ll look at how to import all types with TypeScript.

How to import all types with TypeScript?

To import all types with TypeScript, we can do a namespace import.

For instance, we write

import * as foo from "./otherClass";

to import all members of the ./otherClass module as foo.

Then we can access any member of it from foo like

foo.bar

where bar is an exported member from ./otherClass.

Conclusion

To import all types with TypeScript, we can do a namespace import.

Categories
TypeScript Answers

How to create model classes in TypeScript?

Sometimes, we want to create model classes in TypeScript.

In this article, we’ll look at how to create model classes in TypeScript.

How to create model classes in TypeScript?

To create model classes in TypeScript, we can create interfaces.

For instance, we write

interface IDonutChartModel {
  dimension: number;
  innerRadius: number;
}

const donut: IDonutChartModel = {
  dimension: 1,
  innerRadius: 2,
};

to create the IDonutChartModel interface.

Then we create the donut variable of type IDonutChartModel and assign it an object with content that matches the properties and data types of each property listed in the IDonutChartModel interface.

Conclusion

To create model classes in TypeScript, we can create interfaces.