hasproperty-ts
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

hasproperty-ts

If the object property check is successful, the object type including the target property is generated.

If you do not use this package

function Test(x:unknown){
  if(x instanceof Object && "foo" in x){
    console.log(x.foo); //Error
    console.log((<{foo:unknown}>x).foo); //OK(Cast required)
  }
}

When using this package

import {hasProperty} from "hasproperty-ts";
 
function Test(x: unknown) {
  if (hasProperty(x, "foo")) {
    console.log(x.foo); //TypeScript Type OK
  }
 
  if (hasProperty2(x, "foo", "bar")) {
    console.log(x.foo); //TypeScript Type OK
    console.log(x.bar); //TypeScript Type OK
  }
}

Readme

Keywords

Package Sidebar

Install

npm i hasproperty-ts

Weekly Downloads

0

Version

0.0.3

License

MIT

Unpacked Size

2.32 kB

Total Files

5

Last publish

Collaborators

  • sora_kumo