In the wonderful world of TypeScript, it’s very difficult to access a property of an object that isn’t defined. In fact, TS would throw an error in the compilation build phase informing you of such. I personally love this, however for some it can be inconvenient. Especially if you’re trying to do everything the right way, you know, not use any all over the place. Another case is trying to properly define an object’s indexes through an interface. Well, you’re in luck, that is what keyof can do for you.

Let’s look at the code!

interface TestObject {
  one: string;
  two: string;
  three: string;

const test: Record<keyof TestObject,string> = {
  one: "Hi",
  two: "Hello",
  three: "How are you?",

Easy as can be! This way you can easily have strongly typed object keys, in an convenient and versatile definition.