has-own-key
v1.0.1
Published
```ts type Response = { name: 'name-1' | 'name-2'; };
Downloads
5
Maintainers
Readme
The Typescript utility to typesafely check object keys
Consider this start code
type Response = {
name: 'name-1' | 'name-2';
};
const responses: Response[] = [
{
name: 'name-1',
},
{
name: 'name-2',
},
];
const mapResponses = {
'name-1': 1,
};
Problem, try map responses for client
responses.map((response) => {
if (response.name in mapResponses) {
// ooops, in-guard doesn't work!
return mapResponses[response.name];
}
return response;
});
Solution, use the hasOwnKey predicate function
import hasOwnKey from 'has-own-key';
responses.map((response) => {
if (hasOwnKey(mapResponses, response.name)) {
// now it works!
return mapResponses[response.name];
}
return response;
});