Why am I getting error TS2339 on querySelectorAll for checkbox property 'checked' does not exist











up vote
2
down vote

favorite












I am working on a reactJS app which was created in 2016 and I am trying to patch functionalities to it. The files are in .tsx extension and app compiler looks for all the typescript errors before rendering. So right now I am trying to implement select all checkboxes on clicking the master checkbox input, but as it stands right now I am getting TS2339 error when I call 'checkboxElement.checked = true'.



Master checkbox



 <input type="checkbox" onChange={this.checkAllBoxes} />


Slave checkboxes



<Col lg={1}>
<input type="checkbox" />
</Col>


select all boxes method



checkAllBoxes() {
const allCheckBoxes = document.querySelectorAll("input[type='checkbox']") as NodeListOf<Element>;
allCheckBoxes.forEach(checkBox => {
console.log(checkBox);
if(checkBox && checkBox.checked) checkBox.checked = true;
});
}


As I can see in my console.log that checkBox has checked getter and setter method but I get the TS2339 error on compilation. It's hard for me to believe that I am facing this issue with such a fundamental functionality.



The ERROR: (I get this error logged twice in my compilation window)



error TS2339: Property 'checked' does not exist on type 'Element'.


I have tried changing the query from querySelectorAll to getElementByID and getElementsByClassname with the respective changes. I will prefer querySelectorAll but no strict guidelines. I have looked at this github link where they say they have solved this issue but it doesn't work for me.










share|improve this question




























    up vote
    2
    down vote

    favorite












    I am working on a reactJS app which was created in 2016 and I am trying to patch functionalities to it. The files are in .tsx extension and app compiler looks for all the typescript errors before rendering. So right now I am trying to implement select all checkboxes on clicking the master checkbox input, but as it stands right now I am getting TS2339 error when I call 'checkboxElement.checked = true'.



    Master checkbox



     <input type="checkbox" onChange={this.checkAllBoxes} />


    Slave checkboxes



    <Col lg={1}>
    <input type="checkbox" />
    </Col>


    select all boxes method



    checkAllBoxes() {
    const allCheckBoxes = document.querySelectorAll("input[type='checkbox']") as NodeListOf<Element>;
    allCheckBoxes.forEach(checkBox => {
    console.log(checkBox);
    if(checkBox && checkBox.checked) checkBox.checked = true;
    });
    }


    As I can see in my console.log that checkBox has checked getter and setter method but I get the TS2339 error on compilation. It's hard for me to believe that I am facing this issue with such a fundamental functionality.



    The ERROR: (I get this error logged twice in my compilation window)



    error TS2339: Property 'checked' does not exist on type 'Element'.


    I have tried changing the query from querySelectorAll to getElementByID and getElementsByClassname with the respective changes. I will prefer querySelectorAll but no strict guidelines. I have looked at this github link where they say they have solved this issue but it doesn't work for me.










    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I am working on a reactJS app which was created in 2016 and I am trying to patch functionalities to it. The files are in .tsx extension and app compiler looks for all the typescript errors before rendering. So right now I am trying to implement select all checkboxes on clicking the master checkbox input, but as it stands right now I am getting TS2339 error when I call 'checkboxElement.checked = true'.



      Master checkbox



       <input type="checkbox" onChange={this.checkAllBoxes} />


      Slave checkboxes



      <Col lg={1}>
      <input type="checkbox" />
      </Col>


      select all boxes method



      checkAllBoxes() {
      const allCheckBoxes = document.querySelectorAll("input[type='checkbox']") as NodeListOf<Element>;
      allCheckBoxes.forEach(checkBox => {
      console.log(checkBox);
      if(checkBox && checkBox.checked) checkBox.checked = true;
      });
      }


      As I can see in my console.log that checkBox has checked getter and setter method but I get the TS2339 error on compilation. It's hard for me to believe that I am facing this issue with such a fundamental functionality.



      The ERROR: (I get this error logged twice in my compilation window)



      error TS2339: Property 'checked' does not exist on type 'Element'.


      I have tried changing the query from querySelectorAll to getElementByID and getElementsByClassname with the respective changes. I will prefer querySelectorAll but no strict guidelines. I have looked at this github link where they say they have solved this issue but it doesn't work for me.










      share|improve this question















      I am working on a reactJS app which was created in 2016 and I am trying to patch functionalities to it. The files are in .tsx extension and app compiler looks for all the typescript errors before rendering. So right now I am trying to implement select all checkboxes on clicking the master checkbox input, but as it stands right now I am getting TS2339 error when I call 'checkboxElement.checked = true'.



      Master checkbox



       <input type="checkbox" onChange={this.checkAllBoxes} />


      Slave checkboxes



      <Col lg={1}>
      <input type="checkbox" />
      </Col>


      select all boxes method



      checkAllBoxes() {
      const allCheckBoxes = document.querySelectorAll("input[type='checkbox']") as NodeListOf<Element>;
      allCheckBoxes.forEach(checkBox => {
      console.log(checkBox);
      if(checkBox && checkBox.checked) checkBox.checked = true;
      });
      }


      As I can see in my console.log that checkBox has checked getter and setter method but I get the TS2339 error on compilation. It's hard for me to believe that I am facing this issue with such a fundamental functionality.



      The ERROR: (I get this error logged twice in my compilation window)



      error TS2339: Property 'checked' does not exist on type 'Element'.


      I have tried changing the query from querySelectorAll to getElementByID and getElementsByClassname with the respective changes. I will prefer querySelectorAll but no strict guidelines. I have looked at this github link where they say they have solved this issue but it doesn't work for me.







      javascript reactjs typescript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 14:24

























      asked Nov 8 at 8:45









      Xavitoj Cheema

      3192517




      3192517
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Typescript has no way of knowing the returned HTML Elements are inputs (which do have the checked property.



          The simplest solution is to assert that querySelectorAll returns a list of HTMLInputElement



          const allCheckBoxes = document.querySelectorAll("input[type='checkbox']") as NodeListOf<HTMLInputElement>;
          allCheckBoxes.forEach(checkBox => {
          console.log(checkBox);
          if(checkBox && checkBox.checked) checkBox.checked = true;
          });





          share|improve this answer

















          • 1




            Thanks, this is the right answer. It was my responsibility to understands the expected return from a query like that.
            – Xavitoj Cheema
            Nov 8 at 8:57






          • 1




            done! stackoverflow has a time restricition before I can check it as such.
            – Xavitoj Cheema
            Nov 8 at 9:05


















          up vote
          0
          down vote













          You are casting the allCheckBoxes list to a NodeListOf, but Element has (correctly) no checked property.



          Instead, you should cast it to NodeListOf, which is the proper interface for checkboxes (checked property is defined only for elements)






          share|improve this answer




























            up vote
            0
            down vote













            Iterate as for loop without casting as NodeListOf<Element>.



            checkAllBoxes() {
            const allCheckBoxes = document.querySelectorAll("input[type='checkbox']");
            if(allCheckBoxes){
            for(let i = 0; i< allCheckBoxes.length ; i++){
            console.log(allCheckBoxes[i]);
            if(allCheckBoxes[i] && allCheckBoxes[i].checked) {
            allCheckBoxes[i].checked = true;
            }
            }
            }
            }





            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204174%2fwhy-am-i-getting-error-ts2339-on-queryselectorall-for-checkbox-property-checked%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              2
              down vote



              accepted










              Typescript has no way of knowing the returned HTML Elements are inputs (which do have the checked property.



              The simplest solution is to assert that querySelectorAll returns a list of HTMLInputElement



              const allCheckBoxes = document.querySelectorAll("input[type='checkbox']") as NodeListOf<HTMLInputElement>;
              allCheckBoxes.forEach(checkBox => {
              console.log(checkBox);
              if(checkBox && checkBox.checked) checkBox.checked = true;
              });





              share|improve this answer

















              • 1




                Thanks, this is the right answer. It was my responsibility to understands the expected return from a query like that.
                – Xavitoj Cheema
                Nov 8 at 8:57






              • 1




                done! stackoverflow has a time restricition before I can check it as such.
                – Xavitoj Cheema
                Nov 8 at 9:05















              up vote
              2
              down vote



              accepted










              Typescript has no way of knowing the returned HTML Elements are inputs (which do have the checked property.



              The simplest solution is to assert that querySelectorAll returns a list of HTMLInputElement



              const allCheckBoxes = document.querySelectorAll("input[type='checkbox']") as NodeListOf<HTMLInputElement>;
              allCheckBoxes.forEach(checkBox => {
              console.log(checkBox);
              if(checkBox && checkBox.checked) checkBox.checked = true;
              });





              share|improve this answer

















              • 1




                Thanks, this is the right answer. It was my responsibility to understands the expected return from a query like that.
                – Xavitoj Cheema
                Nov 8 at 8:57






              • 1




                done! stackoverflow has a time restricition before I can check it as such.
                – Xavitoj Cheema
                Nov 8 at 9:05













              up vote
              2
              down vote



              accepted







              up vote
              2
              down vote



              accepted






              Typescript has no way of knowing the returned HTML Elements are inputs (which do have the checked property.



              The simplest solution is to assert that querySelectorAll returns a list of HTMLInputElement



              const allCheckBoxes = document.querySelectorAll("input[type='checkbox']") as NodeListOf<HTMLInputElement>;
              allCheckBoxes.forEach(checkBox => {
              console.log(checkBox);
              if(checkBox && checkBox.checked) checkBox.checked = true;
              });





              share|improve this answer












              Typescript has no way of knowing the returned HTML Elements are inputs (which do have the checked property.



              The simplest solution is to assert that querySelectorAll returns a list of HTMLInputElement



              const allCheckBoxes = document.querySelectorAll("input[type='checkbox']") as NodeListOf<HTMLInputElement>;
              allCheckBoxes.forEach(checkBox => {
              console.log(checkBox);
              if(checkBox && checkBox.checked) checkBox.checked = true;
              });






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 8 at 8:54









              Titian Cernicova-Dragomir

              54k33250




              54k33250








              • 1




                Thanks, this is the right answer. It was my responsibility to understands the expected return from a query like that.
                – Xavitoj Cheema
                Nov 8 at 8:57






              • 1




                done! stackoverflow has a time restricition before I can check it as such.
                – Xavitoj Cheema
                Nov 8 at 9:05














              • 1




                Thanks, this is the right answer. It was my responsibility to understands the expected return from a query like that.
                – Xavitoj Cheema
                Nov 8 at 8:57






              • 1




                done! stackoverflow has a time restricition before I can check it as such.
                – Xavitoj Cheema
                Nov 8 at 9:05








              1




              1




              Thanks, this is the right answer. It was my responsibility to understands the expected return from a query like that.
              – Xavitoj Cheema
              Nov 8 at 8:57




              Thanks, this is the right answer. It was my responsibility to understands the expected return from a query like that.
              – Xavitoj Cheema
              Nov 8 at 8:57




              1




              1




              done! stackoverflow has a time restricition before I can check it as such.
              – Xavitoj Cheema
              Nov 8 at 9:05




              done! stackoverflow has a time restricition before I can check it as such.
              – Xavitoj Cheema
              Nov 8 at 9:05












              up vote
              0
              down vote













              You are casting the allCheckBoxes list to a NodeListOf, but Element has (correctly) no checked property.



              Instead, you should cast it to NodeListOf, which is the proper interface for checkboxes (checked property is defined only for elements)






              share|improve this answer

























                up vote
                0
                down vote













                You are casting the allCheckBoxes list to a NodeListOf, but Element has (correctly) no checked property.



                Instead, you should cast it to NodeListOf, which is the proper interface for checkboxes (checked property is defined only for elements)






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  You are casting the allCheckBoxes list to a NodeListOf, but Element has (correctly) no checked property.



                  Instead, you should cast it to NodeListOf, which is the proper interface for checkboxes (checked property is defined only for elements)






                  share|improve this answer












                  You are casting the allCheckBoxes list to a NodeListOf, but Element has (correctly) no checked property.



                  Instead, you should cast it to NodeListOf, which is the proper interface for checkboxes (checked property is defined only for elements)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 8 at 8:55









                  Keilath

                  1585




                  1585






















                      up vote
                      0
                      down vote













                      Iterate as for loop without casting as NodeListOf<Element>.



                      checkAllBoxes() {
                      const allCheckBoxes = document.querySelectorAll("input[type='checkbox']");
                      if(allCheckBoxes){
                      for(let i = 0; i< allCheckBoxes.length ; i++){
                      console.log(allCheckBoxes[i]);
                      if(allCheckBoxes[i] && allCheckBoxes[i].checked) {
                      allCheckBoxes[i].checked = true;
                      }
                      }
                      }
                      }





                      share|improve this answer

























                        up vote
                        0
                        down vote













                        Iterate as for loop without casting as NodeListOf<Element>.



                        checkAllBoxes() {
                        const allCheckBoxes = document.querySelectorAll("input[type='checkbox']");
                        if(allCheckBoxes){
                        for(let i = 0; i< allCheckBoxes.length ; i++){
                        console.log(allCheckBoxes[i]);
                        if(allCheckBoxes[i] && allCheckBoxes[i].checked) {
                        allCheckBoxes[i].checked = true;
                        }
                        }
                        }
                        }





                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          Iterate as for loop without casting as NodeListOf<Element>.



                          checkAllBoxes() {
                          const allCheckBoxes = document.querySelectorAll("input[type='checkbox']");
                          if(allCheckBoxes){
                          for(let i = 0; i< allCheckBoxes.length ; i++){
                          console.log(allCheckBoxes[i]);
                          if(allCheckBoxes[i] && allCheckBoxes[i].checked) {
                          allCheckBoxes[i].checked = true;
                          }
                          }
                          }
                          }





                          share|improve this answer












                          Iterate as for loop without casting as NodeListOf<Element>.



                          checkAllBoxes() {
                          const allCheckBoxes = document.querySelectorAll("input[type='checkbox']");
                          if(allCheckBoxes){
                          for(let i = 0; i< allCheckBoxes.length ; i++){
                          console.log(allCheckBoxes[i]);
                          if(allCheckBoxes[i] && allCheckBoxes[i].checked) {
                          allCheckBoxes[i].checked = true;
                          }
                          }
                          }
                          }






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 8 at 8:59









                          front_end_dev

                          1,3151511




                          1,3151511






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204174%2fwhy-am-i-getting-error-ts2339-on-queryselectorall-for-checkbox-property-checked%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              這個網誌中的熱門文章

                              Academy of Television Arts & Sciences

                              L'Équipe

                              1995 France bombings