#typescript
Can enforce that something is a particular type with `variable as Type`
However, it does not remove data from objects if they are used as a type. For example:
```typescript
type TestObj = {
foo: string,
}
const objToTest = {
foo: 'buzz',
bar: 2,
}
const typeEnforcedObj = objToTest as TestObj
console.log(typeEnforcedObj)
// output: { foo: 'buzz', bar: 2 }
```
I have been trying to enforce a type and remove properties that don't exist in the type (or interface) but I can't find anything for that in the TypeScript docs, nor do there seem to be snippets online for it.
---
The goal of ensuring an object exactly matches a type can be accomplished if a list of properties in the type is separately defined.
Could be something like this:
```typescript
const includedKeys = ["foo"];
type TestObj = {
foo: string,
};
const objToTest = {
foo: 'buzz',
bar: 2,
};
const enforcedObj = {};
for (var key of includedKeys) {
if (objToTest[key]) {
enforcedObj[key] = objToTest[key];
}
}
console.log( enforcedObj as TestObj );
```
Would still like to find a way to do it without defining the keys in its own array. However, I still cannot find a way to extract the keys from a type.