I have following piece of code
// type def
interface Props: { children: React.ReactNode }
React.Children.map(this.props.children, child => {
// uh-oh!
if (React.isValidElement(child) && child.props.children) {
// do sth
}
}
Which causes typescript error:
semantic error TS2339 Property 'children' does not exist on type '{}'
type of child befor exectution React.isValidElement is React.ReactNodedefined as:
type React.ReactNode =
| string
| number
| boolean
| {}
| React.ReactElement<any>
| React.ReactNodeArray
| React.ReactPortal
| null
| undefined
but after execution of React.isValidElement type script determines child to be React.ReactElement<{}> - it expects element to have empty props which causes an error since it assumes that there is no prop children in the element. Code does not compile.
Am I missing something? is there a way to fix that?
my tsconfig:
"compilerOptions": {
"outDir": "build",
"module": "esnext",
"target": "es5",
"lib": ["es6", "dom", "es2016", "es2017"],
"sourceMap": true,
"allowJs": false,
"jsx": "react",
"declaration": true,
"moduleResolution": "node",
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"removeComments": true,
"preserveConstEnums": true,
"skipLibCheck": true
},
"include": ["./src/**/*"],
}
typescript: 3.2.0
@types/react": ^16.3.13"
@types/react-dom": ^16.0.5"