is-typeof-property

0.2.0 • Public • Published

NPM version Repository package.json version 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":

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:

import isTypeOfProperty from 'is-typeof-property';

if (isTypeOfProperty(value, 'x', 'string')) {
  // Type of `value.y` is now 'string'.
}

Installation

yarn add is-typeof-property
# or
npm install is-typeof-property

License

MIT License.

Readme

Keywords

none

Package Sidebar

Install

npm i is-typeof-property

Weekly Downloads

2

Version

0.2.0

License

MIT

Unpacked Size

7.08 kB

Total Files

7

Last publish

Collaborators

  • vilicvane