https://github.com/vilicvane/is-typeof-property
Simple utilities for tuple type narrowing...
https://github.com/vilicvane/is-typeof-property
Last synced: 4 months 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 3 years ago)
- Default Branch: main
- Last Pushed: 2023-10-22T19:49:53.000Z (over 1 year ago)
- Last Synced: 2025-03-24T00:54:54.119Z (4 months 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
[](https://www.npmjs.com/package/is-typeof-property)
[](./package.json)
[](./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.