Home > Enterprise >  How to resolve type error UseRef's current?
How to resolve type error UseRef's current?

Time:08-10

I am struggling with type error from UseRef's current.
Here is my code, the error happens in the last line (idnList.current)
[ ERROR CODE ] TS2322: Type 'IdnListProps[]' is not assignable to type 'never[]'.

const idnList = useRef([]);  
 
const _idnList: ({id:string, label:string})[] = [];
 if (_defaultIdn) { idnList.push({id:"id",label:"label"}) } 
idnList.current = _idnList;

How can I solve this type error?

CodePudding user response:

Try to specify this type in useRef

const idnList = useRef<Array<{id:string, label:string}>>([])
 const _idnList: ({id:string, label:string})[] = [];
        if (_defaultIdn) {
           _idnList.push({id,label})
         } 
   idnList.current = _idnList;
  • Related