How excess property check helps?











up vote
1
down vote

favorite












For the below code,



interface SquareConfig{
color?: string;
width?: number;
}

interface Square{
color: string;
area: number;
}

function createSquare(config: SquareConfig): Square {

let newSquare:Square = {color: "white", area: 100};
if (config.color) {
newSquare.color = config.color;
}
if (config.width) {
newSquare.area = config.width * config.width;
}
return newSquare;
}


below argument(myObj) inferred as type any is allowed to pass as argument by type checker at compile time. JS code use duck typing at runtime.



let myObj = {colour: 'red', width: 100};

let mySquare = createSquare(myObj);


In second case, below argument(other thanSquareConfig type) is not allowed to pass by type checker at compile time. As mentioned in handbook: Object literals get special treatment and undergo excess property checking when assigning them to other variables, or passing them as arguments.



let mySquare = createSquare({colour: 'red', width: 100});


What is the purpose of excess property check, in second case?










share|improve this question




























    up vote
    1
    down vote

    favorite












    For the below code,



    interface SquareConfig{
    color?: string;
    width?: number;
    }

    interface Square{
    color: string;
    area: number;
    }

    function createSquare(config: SquareConfig): Square {

    let newSquare:Square = {color: "white", area: 100};
    if (config.color) {
    newSquare.color = config.color;
    }
    if (config.width) {
    newSquare.area = config.width * config.width;
    }
    return newSquare;
    }


    below argument(myObj) inferred as type any is allowed to pass as argument by type checker at compile time. JS code use duck typing at runtime.



    let myObj = {colour: 'red', width: 100};

    let mySquare = createSquare(myObj);


    In second case, below argument(other thanSquareConfig type) is not allowed to pass by type checker at compile time. As mentioned in handbook: Object literals get special treatment and undergo excess property checking when assigning them to other variables, or passing them as arguments.



    let mySquare = createSquare({colour: 'red', width: 100});


    What is the purpose of excess property check, in second case?










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      For the below code,



      interface SquareConfig{
      color?: string;
      width?: number;
      }

      interface Square{
      color: string;
      area: number;
      }

      function createSquare(config: SquareConfig): Square {

      let newSquare:Square = {color: "white", area: 100};
      if (config.color) {
      newSquare.color = config.color;
      }
      if (config.width) {
      newSquare.area = config.width * config.width;
      }
      return newSquare;
      }


      below argument(myObj) inferred as type any is allowed to pass as argument by type checker at compile time. JS code use duck typing at runtime.



      let myObj = {colour: 'red', width: 100};

      let mySquare = createSquare(myObj);


      In second case, below argument(other thanSquareConfig type) is not allowed to pass by type checker at compile time. As mentioned in handbook: Object literals get special treatment and undergo excess property checking when assigning them to other variables, or passing them as arguments.



      let mySquare = createSquare({colour: 'red', width: 100});


      What is the purpose of excess property check, in second case?










      share|improve this question















      For the below code,



      interface SquareConfig{
      color?: string;
      width?: number;
      }

      interface Square{
      color: string;
      area: number;
      }

      function createSquare(config: SquareConfig): Square {

      let newSquare:Square = {color: "white", area: 100};
      if (config.color) {
      newSquare.color = config.color;
      }
      if (config.width) {
      newSquare.area = config.width * config.width;
      }
      return newSquare;
      }


      below argument(myObj) inferred as type any is allowed to pass as argument by type checker at compile time. JS code use duck typing at runtime.



      let myObj = {colour: 'red', width: 100};

      let mySquare = createSquare(myObj);


      In second case, below argument(other thanSquareConfig type) is not allowed to pass by type checker at compile time. As mentioned in handbook: Object literals get special treatment and undergo excess property checking when assigning them to other variables, or passing them as arguments.



      let mySquare = createSquare({colour: 'red', width: 100});


      What is the purpose of excess property check, in second case?







      typescript ecmascript-6 duck-typing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 2 at 20:56

























      asked May 2 at 20:41









      overexchange

      3,49462571




      3,49462571
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted











          What is the purpose of excess property check, in second case?




          It correctly detects bugs (as shown in this case, the misspelling of color) without creating too many false positives.



          Because the object isn't aliased anywhere else, TypeScript can be fairly confident that the excess property isn't going to be used for a different purpose in some other part of the code. The same cannot be said of myObj - we may be inspecting it only for its .width here but then using its .colour in some other place.






          share|improve this answer





















          • It is weird that object literal has excess property check, but not object myObj. Why is that?
            – overexchange
            May 3 at 1:18






          • 1




            Because the object isn't aliased anywhere else, TypeScript can be fairly confident that the excess property isn't going to be used for a different purpose in some other part of the code. The same cannot be said of myObj - we may be inspecting it only for its .width here but then using its .colour in some other place.
            – Ryan Cavanaugh
            May 3 at 5:21










          • In case of myObj, you know at compile time that it is of type any, then how would you allow it to be passed to function which accepts SquareConfig type?
            – overexchange
            May 3 at 8:25










          • myObj is not of type any.
            – Ryan Cavanaugh
            May 3 at 8:57










          • colour property in myObj makes it of type any. Isn't it?
            – overexchange
            May 3 at 21:27













          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%2f50143250%2fhow-excess-property-check-helps%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote



          accepted











          What is the purpose of excess property check, in second case?




          It correctly detects bugs (as shown in this case, the misspelling of color) without creating too many false positives.



          Because the object isn't aliased anywhere else, TypeScript can be fairly confident that the excess property isn't going to be used for a different purpose in some other part of the code. The same cannot be said of myObj - we may be inspecting it only for its .width here but then using its .colour in some other place.






          share|improve this answer





















          • It is weird that object literal has excess property check, but not object myObj. Why is that?
            – overexchange
            May 3 at 1:18






          • 1




            Because the object isn't aliased anywhere else, TypeScript can be fairly confident that the excess property isn't going to be used for a different purpose in some other part of the code. The same cannot be said of myObj - we may be inspecting it only for its .width here but then using its .colour in some other place.
            – Ryan Cavanaugh
            May 3 at 5:21










          • In case of myObj, you know at compile time that it is of type any, then how would you allow it to be passed to function which accepts SquareConfig type?
            – overexchange
            May 3 at 8:25










          • myObj is not of type any.
            – Ryan Cavanaugh
            May 3 at 8:57










          • colour property in myObj makes it of type any. Isn't it?
            – overexchange
            May 3 at 21:27

















          up vote
          4
          down vote



          accepted











          What is the purpose of excess property check, in second case?




          It correctly detects bugs (as shown in this case, the misspelling of color) without creating too many false positives.



          Because the object isn't aliased anywhere else, TypeScript can be fairly confident that the excess property isn't going to be used for a different purpose in some other part of the code. The same cannot be said of myObj - we may be inspecting it only for its .width here but then using its .colour in some other place.






          share|improve this answer





















          • It is weird that object literal has excess property check, but not object myObj. Why is that?
            – overexchange
            May 3 at 1:18






          • 1




            Because the object isn't aliased anywhere else, TypeScript can be fairly confident that the excess property isn't going to be used for a different purpose in some other part of the code. The same cannot be said of myObj - we may be inspecting it only for its .width here but then using its .colour in some other place.
            – Ryan Cavanaugh
            May 3 at 5:21










          • In case of myObj, you know at compile time that it is of type any, then how would you allow it to be passed to function which accepts SquareConfig type?
            – overexchange
            May 3 at 8:25










          • myObj is not of type any.
            – Ryan Cavanaugh
            May 3 at 8:57










          • colour property in myObj makes it of type any. Isn't it?
            – overexchange
            May 3 at 21:27















          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted







          What is the purpose of excess property check, in second case?




          It correctly detects bugs (as shown in this case, the misspelling of color) without creating too many false positives.



          Because the object isn't aliased anywhere else, TypeScript can be fairly confident that the excess property isn't going to be used for a different purpose in some other part of the code. The same cannot be said of myObj - we may be inspecting it only for its .width here but then using its .colour in some other place.






          share|improve this answer













          What is the purpose of excess property check, in second case?




          It correctly detects bugs (as shown in this case, the misspelling of color) without creating too many false positives.



          Because the object isn't aliased anywhere else, TypeScript can be fairly confident that the excess property isn't going to be used for a different purpose in some other part of the code. The same cannot be said of myObj - we may be inspecting it only for its .width here but then using its .colour in some other place.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 2 at 22:07









          Ryan Cavanaugh

          92.4k27159170




          92.4k27159170












          • It is weird that object literal has excess property check, but not object myObj. Why is that?
            – overexchange
            May 3 at 1:18






          • 1




            Because the object isn't aliased anywhere else, TypeScript can be fairly confident that the excess property isn't going to be used for a different purpose in some other part of the code. The same cannot be said of myObj - we may be inspecting it only for its .width here but then using its .colour in some other place.
            – Ryan Cavanaugh
            May 3 at 5:21










          • In case of myObj, you know at compile time that it is of type any, then how would you allow it to be passed to function which accepts SquareConfig type?
            – overexchange
            May 3 at 8:25










          • myObj is not of type any.
            – Ryan Cavanaugh
            May 3 at 8:57










          • colour property in myObj makes it of type any. Isn't it?
            – overexchange
            May 3 at 21:27




















          • It is weird that object literal has excess property check, but not object myObj. Why is that?
            – overexchange
            May 3 at 1:18






          • 1




            Because the object isn't aliased anywhere else, TypeScript can be fairly confident that the excess property isn't going to be used for a different purpose in some other part of the code. The same cannot be said of myObj - we may be inspecting it only for its .width here but then using its .colour in some other place.
            – Ryan Cavanaugh
            May 3 at 5:21










          • In case of myObj, you know at compile time that it is of type any, then how would you allow it to be passed to function which accepts SquareConfig type?
            – overexchange
            May 3 at 8:25










          • myObj is not of type any.
            – Ryan Cavanaugh
            May 3 at 8:57










          • colour property in myObj makes it of type any. Isn't it?
            – overexchange
            May 3 at 21:27


















          It is weird that object literal has excess property check, but not object myObj. Why is that?
          – overexchange
          May 3 at 1:18




          It is weird that object literal has excess property check, but not object myObj. Why is that?
          – overexchange
          May 3 at 1:18




          1




          1




          Because the object isn't aliased anywhere else, TypeScript can be fairly confident that the excess property isn't going to be used for a different purpose in some other part of the code. The same cannot be said of myObj - we may be inspecting it only for its .width here but then using its .colour in some other place.
          – Ryan Cavanaugh
          May 3 at 5:21




          Because the object isn't aliased anywhere else, TypeScript can be fairly confident that the excess property isn't going to be used for a different purpose in some other part of the code. The same cannot be said of myObj - we may be inspecting it only for its .width here but then using its .colour in some other place.
          – Ryan Cavanaugh
          May 3 at 5:21












          In case of myObj, you know at compile time that it is of type any, then how would you allow it to be passed to function which accepts SquareConfig type?
          – overexchange
          May 3 at 8:25




          In case of myObj, you know at compile time that it is of type any, then how would you allow it to be passed to function which accepts SquareConfig type?
          – overexchange
          May 3 at 8:25












          myObj is not of type any.
          – Ryan Cavanaugh
          May 3 at 8:57




          myObj is not of type any.
          – Ryan Cavanaugh
          May 3 at 8:57












          colour property in myObj makes it of type any. Isn't it?
          – overexchange
          May 3 at 21:27






          colour property in myObj makes it of type any. Isn't it?
          – overexchange
          May 3 at 21:27




















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50143250%2fhow-excess-property-check-helps%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







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini