TypeScript: Only Allow A Property From Interface as Parameter
Tags: typescript, javascript, programming
Published: 2022-04-25 08:41:58

---
title: TypeScript: Only Allow A Property From Interface as Parameter
published: true
date: 2022-04-20 09:04:42 UTC
tags: TypeScript, JavaScript, Programming
canonical_url: https://hasnode.byrayray.dev/typescript-only-allow-a-property-from-interface-as-parameter
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8m445c09dk0ci7pqogcj.jpg
---



So lately I was working with a team member on an Angular Pipe. We had an Address Interface and wanted to only allow an argument that was property from the Adress Interface. But how do you define that?

It turns out very simple, we need to use the keyof operator!

![divider-byrayray.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629890886208/NhHYvPmBA.png)

## Interface

First, we have to check the Address interface. Below is a more simplified version of what we use, but this is perfect for this example.

```
interface Address {
    streetname: string;
    housnumber: number;
    postal: string;
    city: string;
}

```

![divider-byrayray.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629890886208/NhHYvPmBA.png)

## Use keyof operator

With the [keyof operator](https://www.typescriptlang.org/docs/handbook/2/keyof-types.html), we can say that a parameter should be equal to the property of an interface.

```
function fakeFunc(param: keyof Address): void {
    console.log(param);
}

```

Now we can ensure that this function is only being used with a parameter that exists in the Address interface. Otherwise, you will get a TypeScript interface.

![divider-byrayray.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629890886208/NhHYvPmBA.png)

## Validate the function

In the [example](https://codesandbox.io/embed/musing-haslett-eiyzup?expanddevtools=1&fontsize=14&hidenavigation=1&theme=dark&view=editor) you can see that the param ‘streetname’ is accepted but the parameter ‘random’ is giving the error “_Argument of type '"random"' is not assignable to parameter of type 'keyof Address'._” because it’s not in the interface. (_To see the compile error, you need to click the check it on [CodeSandbox](https://codesandbox.io/embed/musing-haslett-eiyzup?expanddevtools=1&fontsize=14&hidenavigation=1&theme=dark&view=editor)_)

{% embed https://codesandbox.io/embed/musing-haslett-eiyzup?expanddevtools=1&fontsize=14&hidenavigation=1&theme=dark&view=editor %}

![divider-byrayray.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629890886208/NhHYvPmBA.png)

## Thanks!

![hashnode-footer.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629789655319/nBF6anHH4w.png)_After reading this story, I hope you learned something new or are inspired to create something new! 🤗 If so, consider subscribing via email (scroll to the top of this page) or follow me here on Hashnode._

> Did you know that you can create a [Developer blog like this one, yourself](https://hashnode.com/@devbyrayray/joinme)? It's entirely for free. 👍💰🎉🥳🔥

_If I left you with questions or something to say as a response, scroll down and type me a message. Please send me a [DM on Twitter @DevByRayRay](https://twitter.com/@devbyrayray) when you want to keep it private. My DM's are always open 😁_