Can anyone help me with this strange issue I'm experiencing in Typescript? I keep getting an error about a potential undefined value:
https://i.sstatic.net/GaLv7.png
if (!flatListRef || !flatListRef.current) return
flatListRef.current.someMethod()
The same error persists with this code snippet as well:
if (flatListRef && flatListRef.current) {
flatListRef.current.someMethod()
}
And even with this one:
flatListRef?.current?.someMethod()
Also, this variation triggers the same TS2532 error message:
flatListRef!.current!.someMethod()
I am using Typescript 4.3.5 and targeting es6
. I have updated all my packages, but I am still unable to troubleshoot this problem effectively. Any suggestions or hints would be highly appreciated :-).
Here is a snippet of my tsconfig.json file:
{
"compilerOptions": {
"allowJs": false,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"jsx": "react-native",
"esModuleInterop": true,
"module": "es2015",
"moduleResolution": "node",
"noImplicitAny": false,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"sourceMap": true,
"target": "es6",
"lib": [
"esnext",
"dom"
],
"skipLibCheck": true
},
"exclude": [
"node_modules"
],
"include": [
"App.js",
"app",
"test",
"storybook"
]
}