简体   繁体   中英

Property 'level' does not exist on type 'never[]'.ts(2339) but it does log

I'm unsure why this is giving me an error I know there's something I'm meant to do but I can't figure out what to tell it not to give me an error. If I run it, it gives me an error BUT it does actually still work so the value does log in the console.

const Tab1: React.FC = () => {
  const [lockdownData, setLockdownData] = useState();
  
  useEffect(() => {
    let finalLockdownData: any;
    var getLockdownData = async () => {
      await firestoreDB.collection("LockdownStatus").doc("lockdownData").get().then((doc) => {
        finalLockdownData = doc.data();
      });
      setLockdownData(finalLockdownData);
    };
    getLockdownData();
  }, []);
  
  console.log('Position 1')
  console.log(lockdownData?.level) //ERROR: Property 'level' does not exist on type 'never[]'.ts(2339)
  console.log('Position 2')

  return (

Just write it like this

const [lockdownData, setLockdownData] = useState<any[]>([])

Edit 1

if (lockdownData === null) {
    console.log('data is nullable');
} else {
    console.log(lockdownData!.level);
}

Edit 2

const [lockdownData, setLockdownData] = useState<Record<string, never>>({});

Codesandbox: https://codesandbox.io/s/dreamy-water-v0vwb?file=/src/App.tsx:108-184

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM