Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vilicvane/is-typeof-property
Simple utilities for tuple type narrowing...
https://github.com/vilicvane/is-typeof-property
Last synced: 20 days ago
JSON representation
Simple utilities for tuple type narrowing...
- Host: GitHub
- URL: https://github.com/vilicvane/is-typeof-property
- Owner: vilicvane
- License: mit
- Created: 2022-04-04T09:31:06.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-10-22T19:49:53.000Z (about 1 year ago)
- Last Synced: 2024-11-30T15:41:56.789Z (23 days ago)
- Language: TypeScript
- Size: 62.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![NPM version](https://img.shields.io/npm/v/is-typeof-property?color=%23cb3837&style=flat-square)](https://www.npmjs.com/package/is-typeof-property)
[![Repository package.json version](https://img.shields.io/github/package-json/v/vilic/is-typeof-property?color=%230969da&label=repo&style=flat-square)](./package.json)
[![License](https://img.shields.io/github/license/vilic/is-typeof-property?style=flat-square)](./LICENSE)# isTypeOfProperty(object, key, type)
> Simple utility for `if (typeof object[property]) ...` type narrowing.
TypeScript as of version 4.6 does not support discriminated union based on the type of properties, so the code below does not work as "expected":
```ts
let value!: {x: string; y: 'string'} | {x: number; y: 'number'};if (typeof value.x === 'string') {
// Expecting `value.y` to be 'string', but gets 'string' | 'number'.
}
```With this utility function:
```ts
import isTypeOfProperty from 'is-typeof-property';if (isTypeOfProperty(value, 'x', 'string')) {
// Type of `value.y` is now 'string'.
}
```## Installation
```sh
yarn add is-typeof-property
# or
npm install is-typeof-property
```## License
MIT License.