Typescript, how to pass “Object is possibly null” error?

前端 未结 5 1870
谎友^
谎友^ 2021-02-05 00:16

I\'ve got the \"Object is possibly null\" error many times and usually I use a safety \"if statement\" in case it returns null.

I\'ve got the following function:

<
相关标签:
5条回答
  • 2021-02-05 00:31

    When you declare const overlayEl = useRef(null); Makes the type it comes out as is null because that's the best possible inference it can offer with that much information, give typescript more information and it will work as intended.

    Try....

     const overlayEl = useRef<HTMLDivElement>(null);
    

    Alternatively some syntax sugar for if you don't care for when its undefined is to do something like this.

    const overlayEl = useRef(document.createElement("div"))
    

    using the above syntax all common DOM methods just return defaults such as "0" i.e overlayEl.offsetWidth, getBoundingClientRect etc.

    Usage:

    if(overlayEl.current) {
        // will be type HTMLDivElement NOT HTMLDivElement | null
        const whattype = overlayEl.current; 
    }
    

    The way this works is typescripts static analysis is smart enough to figure out that if check "guards" against null, and therefore it will remove that as a possible type from the union of null | HTMLDivElement within those brackets.

    0 讨论(0)
  • 2021-02-05 00:40

    If you want to "pass/skip" then this will do it const overlayEl: any = useRef(null);

    0 讨论(0)
  • 2021-02-05 00:44
    const overlayEl = useRef() as MutableRefObject<HTMLDivElement>;
    

    It will cast overlayEl to an initiated MutableRefObject that is the returning value of useRef:

    function useRef<T = undefined>(): MutableRefObject<T | undefined>;
    

    Yet in this case, the compiler will always think that overlayEl has a value.

    0 讨论(0)
  • 2021-02-05 00:56

    Add a type to the ref as mentioned by @Shanon Jackson:

    const linkRef = useRef<HTMLLinkElement>(null);
    

    And then, make sure you check for null value before using current:

    if (linkRef.current !== null) {
      linkRef.current.focus();
    }
    

    This will satisfy Typescript. Whereas either by itself wouldn't.

    Using any or casting in order to "trick" the compiler defeats the purpose of using Typescript, don't do that.

    0 讨论(0)
  • 2021-02-05 00:56

    If you really know that in executing time you dont have a error here then just put :

     (overlayEl as any).current 
    

    If not, better use:

        if (typeof overlayEl !== 'undefined' &&
          typeof overlayEl.current !== 'undefined' &&
          overlayEl.current === null) {          
          return;
        }
    
        // Or
    
        try {
          // you code here ...
          // This is fine way to check by order -> parent.parent.FinalInstance
          // Also try & catch will handle all bad situation about current error
          overlay &&  overlayEl.current && overlayEl.current.focus();
    
        } catch(e){
           console.log("Real null >> ", e);     
        }
    
      // Suggest if i am wrong in syntax somewhere ,this is fast answer ;)
    
    0 讨论(0)
提交回复
热议问题