@sounisi5011/ts-utils-is-property-accessible
Version:
Check if a value is property accessible
72 lines (55 loc) • 3.05 kB
Markdown
# @sounisi5011/ts-utils-is-property-accessible
[](https://www.npmjs.com/package/@sounisi5011/ts-utils-is-property-accessible)
[](https://github.com/facebook/jest)
[](http://commitizen.github.io/cz-cli/)
[](https://bundlephobia.com/result?p=%40sounisi5011%2Fts-utils-is-property-accessible%401.0.3)
[](https://packagephobia.com/result?p=%40sounisi5011%2Fts-utils-is-property-accessible%401.0.3)
[](https://david-dm.org/sounisi5011/npm-packages?path=packages%2Fts-utils%2Fis-property-accessible)
[](https://github.com/sounisi5011/npm-packages/actions/workflows/ci.yaml)
[](https://codeclimate.com/github/sounisi5011/npm-packages/maintainability)
Check if a value is property accessible.
## Installation
```sh
npm install @sounisi5011/ts-utils-is-property-accessible
```
```sh
yarn add @sounisi5011/ts-utils-is-property-accessible
```
```sh
pnpm add @sounisi5011/ts-utils-is-property-accessible
```
## Usage
### TypeScript
```ts
import { isPropertyAccessible } from '@sounisi5011/ts-utils-is-property-accessible';
if (isPropertyAccessible(value)) {
// `value.anyProperty` is available!
// Because the value is neither null nor undefined.
// In addition, an index signature type will be added so that any property can be read.
} else {
// If you try to read or write any property, a TypeError will probably be thrown.
// Because the value is null or undefined.
}
```
### JavaScript (ES Modules)
```js
import { isPropertyAccessible } from '@sounisi5011/ts-utils-is-property-accessible';
if (isPropertyAccessible(value)) {
// `value.anyProperty` is available!
// Because the value is neither null nor undefined.
} else {
// If you try to read or write any property, a TypeError will probably be thrown.
// Because the value is null or undefined.
}
```
### JavaScript (CommonJS)
```js
const { isPropertyAccessible } = require('@sounisi5011/ts-utils-is-property-accessible');
if (isPropertyAccessible(value)) {
// `value.anyProperty` is available!
// Because the value is neither null nor undefined.
} else {
// If you try to read or write any property, a TypeError will probably be thrown.
// Because the value is null or undefined.
}
```