Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6208

Why hasOwnProperty method doesn't eliminate {} type

$
0
0

I have a complex type ReactNode from @types/react 16.9.17 version and TypeScript v3.7.4 version.

import React, { ReactNode } from 'react';

and I want to reduce this type to type, which has property children.

I'm trying to achieve this using implicit type-guards:

import React, { ReactNode } from 'react';

function func(x: ReactNode) {
  if (typeof x === 'object'&& x !== null && x.hasOwnProperty("children")) {
    x.children
  }
}

but I see error Property 'children' does not exist on type '{}'.

Questions:

  1. Why I see this error (why typescript doesn't eliminate empty {} type after hasOwnProperty() check)?

  2. How in general eliminate {} type from {} | Type types?

  3. How to reduce ReactNode type to type, having children property?

enter image description here


Viewing all articles
Browse latest Browse all 6208

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>