How to model interrelated attributes in Typescript












2















I have a set of attributes, all calculable from each other:



 A = B * C
B = A / C
C = A / B


A, B, and C are all attributes of my model, so there is a function which takes a model that is missing one of them and returns a model which has all three specified.



Thus, there is a type-concept of a "solvable" model, which is one that is missing 0 or 1 attributes, and an "unsolvable" model, which is missing 2 or more. That is, a solvable model is one that can be converted, via one of the above functions (or without making any change), into a complete model.



How can I model these concepts in my type system? So far, I've tried using Partial or Pick to create individual types for each of these, but it was incredibly verbose and I couldn't get other parts of my app which consumed these functions to correctly compile.










share|improve this question



























    2















    I have a set of attributes, all calculable from each other:



     A = B * C
    B = A / C
    C = A / B


    A, B, and C are all attributes of my model, so there is a function which takes a model that is missing one of them and returns a model which has all three specified.



    Thus, there is a type-concept of a "solvable" model, which is one that is missing 0 or 1 attributes, and an "unsolvable" model, which is missing 2 or more. That is, a solvable model is one that can be converted, via one of the above functions (or without making any change), into a complete model.



    How can I model these concepts in my type system? So far, I've tried using Partial or Pick to create individual types for each of these, but it was incredibly verbose and I couldn't get other parts of my app which consumed these functions to correctly compile.










    share|improve this question

























      2












      2








      2


      1






      I have a set of attributes, all calculable from each other:



       A = B * C
      B = A / C
      C = A / B


      A, B, and C are all attributes of my model, so there is a function which takes a model that is missing one of them and returns a model which has all three specified.



      Thus, there is a type-concept of a "solvable" model, which is one that is missing 0 or 1 attributes, and an "unsolvable" model, which is missing 2 or more. That is, a solvable model is one that can be converted, via one of the above functions (or without making any change), into a complete model.



      How can I model these concepts in my type system? So far, I've tried using Partial or Pick to create individual types for each of these, but it was incredibly verbose and I couldn't get other parts of my app which consumed these functions to correctly compile.










      share|improve this question














      I have a set of attributes, all calculable from each other:



       A = B * C
      B = A / C
      C = A / B


      A, B, and C are all attributes of my model, so there is a function which takes a model that is missing one of them and returns a model which has all three specified.



      Thus, there is a type-concept of a "solvable" model, which is one that is missing 0 or 1 attributes, and an "unsolvable" model, which is missing 2 or more. That is, a solvable model is one that can be converted, via one of the above functions (or without making any change), into a complete model.



      How can I model these concepts in my type system? So far, I've tried using Partial or Pick to create individual types for each of these, but it was incredibly verbose and I couldn't get other parts of my app which consumed these functions to correctly compile.







      typescript types






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 18 '18 at 19:19









      ABMagilABMagil

      1,28311026




      1,28311026
























          1 Answer
          1






          active

          oldest

          votes


















          2














          I'm not sure if the following counts as "incredibly verbose" (it uses Pick<> internally, so maybe?) or if it runs into the same compilation problems you saw, but:



          type MissingOneProperty<O extends object> = {
          [K in keyof O]: Pick<O, Exclude<keyof O, K>>
          }[keyof O];
          type MissingAtMostOneProperty<O extends object> =
          O | MissingOneProperty<O>;


          The idea is that MissingAtMostOneProperty<O> is either O or it is missing exactly one property from O. This probably only works for object types without index signatures (do you care?).



          So if I define your model like this:



          interface Model {
          a: number,
          b: number,
          c: number
          }


          I can declare a function that only accepts models missing at most one property:



          declare function solveModel(
          solvableModel: MissingAtMostOneProperty<Model>
          ): Model;
          solveModel({ a: 1, b: 2 }); // okay
          solveModel({ b: 2, c: 0.5 }); // okay
          solveModel({ a: 1, c: 0.5 }); // okay
          solveModel({ a: 1, b: 2, c: 0.5 }); // okay
          solveModel({ a: 1 }); // error, property "b" is missing
          solveModel({ b: 2 }); // error, property "a" is missing
          solveModel({ c: 0.5 }); // error, property "a" is missing
          solveModel({}); // error, property "a" is missing


          Looks reasonable to me.





          To understand how that works, let's walk through what MissingAtMostOneProperty<Model> gets evaluated to:



          MissingAtMostOneProperty<Model> 


          becomes, by the definition of MissingAtMostOneProperty:



          Model | MissingOneProperty<Model>


          which is, by the definition of MissingOneProperty:



          Model | {[K in keyof Model]: Pick<Model, Exclude<keyof Model, K>>}[keyof Model]


          which is, by mapping over the 'a', 'b', and 'c' properties of Model:



          Model | {
          a: Pick<Model, Exclude<keyof Model, 'a'>,
          b: Pick<Model, Exclude<keyof Model, 'b'>,
          c: Pick<Model, Exclude<keyof Model, 'c'>
          }[keyof Model]


          which is, by noting that keyof Model is 'a'|'b'|'c' and that Exclude<T, U> is a conditional type that removes elements from unions:



          Model | {
          a: Pick<Model, 'b'|'c'>,
          b: Pick<Model, 'a'|'c'>,
          c: Pick<Model, 'a'|'b'>
          }['a'|'b'|'c']


          which, by noting how Pick<> works, becomes:



          Model | {
          a: { b: number, c: number },
          b: { a: number, c: number },
          c: { a: number, b: number }
          }['a'|'b'|'c']


          which, finally, by noting that looking up a union of property keys in a type is the same as the union of the types of the properties, and by the definition of Model, turns into:



          {a: number, b: number, c: number} 
          | { b: number, c: number }
          | { a: number, c: number }
          | { a: number, b: number }


          Done! You can see how you end up with a union of Model and all ways of removing one property from Model.



          Hope that gives you some direction. Good luck!






          share|improve this answer


























          • The verbosity was in creating ModelWithoutA, ModelWithoutB, etc. types. Not just in using Pick. This is neat, but I can't quite piece together what MissingOneProperty<> does.

            – ABMagil
            Nov 19 '18 at 1:18






          • 1





            Edited: tried explaining it a bit by walking through how it gets evaluated.

            – jcalz
            Nov 19 '18 at 1:38











          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',
          autoActivateHeartbeat: false,
          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%2f53364579%2fhow-to-model-interrelated-attributes-in-typescript%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









          2














          I'm not sure if the following counts as "incredibly verbose" (it uses Pick<> internally, so maybe?) or if it runs into the same compilation problems you saw, but:



          type MissingOneProperty<O extends object> = {
          [K in keyof O]: Pick<O, Exclude<keyof O, K>>
          }[keyof O];
          type MissingAtMostOneProperty<O extends object> =
          O | MissingOneProperty<O>;


          The idea is that MissingAtMostOneProperty<O> is either O or it is missing exactly one property from O. This probably only works for object types without index signatures (do you care?).



          So if I define your model like this:



          interface Model {
          a: number,
          b: number,
          c: number
          }


          I can declare a function that only accepts models missing at most one property:



          declare function solveModel(
          solvableModel: MissingAtMostOneProperty<Model>
          ): Model;
          solveModel({ a: 1, b: 2 }); // okay
          solveModel({ b: 2, c: 0.5 }); // okay
          solveModel({ a: 1, c: 0.5 }); // okay
          solveModel({ a: 1, b: 2, c: 0.5 }); // okay
          solveModel({ a: 1 }); // error, property "b" is missing
          solveModel({ b: 2 }); // error, property "a" is missing
          solveModel({ c: 0.5 }); // error, property "a" is missing
          solveModel({}); // error, property "a" is missing


          Looks reasonable to me.





          To understand how that works, let's walk through what MissingAtMostOneProperty<Model> gets evaluated to:



          MissingAtMostOneProperty<Model> 


          becomes, by the definition of MissingAtMostOneProperty:



          Model | MissingOneProperty<Model>


          which is, by the definition of MissingOneProperty:



          Model | {[K in keyof Model]: Pick<Model, Exclude<keyof Model, K>>}[keyof Model]


          which is, by mapping over the 'a', 'b', and 'c' properties of Model:



          Model | {
          a: Pick<Model, Exclude<keyof Model, 'a'>,
          b: Pick<Model, Exclude<keyof Model, 'b'>,
          c: Pick<Model, Exclude<keyof Model, 'c'>
          }[keyof Model]


          which is, by noting that keyof Model is 'a'|'b'|'c' and that Exclude<T, U> is a conditional type that removes elements from unions:



          Model | {
          a: Pick<Model, 'b'|'c'>,
          b: Pick<Model, 'a'|'c'>,
          c: Pick<Model, 'a'|'b'>
          }['a'|'b'|'c']


          which, by noting how Pick<> works, becomes:



          Model | {
          a: { b: number, c: number },
          b: { a: number, c: number },
          c: { a: number, b: number }
          }['a'|'b'|'c']


          which, finally, by noting that looking up a union of property keys in a type is the same as the union of the types of the properties, and by the definition of Model, turns into:



          {a: number, b: number, c: number} 
          | { b: number, c: number }
          | { a: number, c: number }
          | { a: number, b: number }


          Done! You can see how you end up with a union of Model and all ways of removing one property from Model.



          Hope that gives you some direction. Good luck!






          share|improve this answer


























          • The verbosity was in creating ModelWithoutA, ModelWithoutB, etc. types. Not just in using Pick. This is neat, but I can't quite piece together what MissingOneProperty<> does.

            – ABMagil
            Nov 19 '18 at 1:18






          • 1





            Edited: tried explaining it a bit by walking through how it gets evaluated.

            – jcalz
            Nov 19 '18 at 1:38
















          2














          I'm not sure if the following counts as "incredibly verbose" (it uses Pick<> internally, so maybe?) or if it runs into the same compilation problems you saw, but:



          type MissingOneProperty<O extends object> = {
          [K in keyof O]: Pick<O, Exclude<keyof O, K>>
          }[keyof O];
          type MissingAtMostOneProperty<O extends object> =
          O | MissingOneProperty<O>;


          The idea is that MissingAtMostOneProperty<O> is either O or it is missing exactly one property from O. This probably only works for object types without index signatures (do you care?).



          So if I define your model like this:



          interface Model {
          a: number,
          b: number,
          c: number
          }


          I can declare a function that only accepts models missing at most one property:



          declare function solveModel(
          solvableModel: MissingAtMostOneProperty<Model>
          ): Model;
          solveModel({ a: 1, b: 2 }); // okay
          solveModel({ b: 2, c: 0.5 }); // okay
          solveModel({ a: 1, c: 0.5 }); // okay
          solveModel({ a: 1, b: 2, c: 0.5 }); // okay
          solveModel({ a: 1 }); // error, property "b" is missing
          solveModel({ b: 2 }); // error, property "a" is missing
          solveModel({ c: 0.5 }); // error, property "a" is missing
          solveModel({}); // error, property "a" is missing


          Looks reasonable to me.





          To understand how that works, let's walk through what MissingAtMostOneProperty<Model> gets evaluated to:



          MissingAtMostOneProperty<Model> 


          becomes, by the definition of MissingAtMostOneProperty:



          Model | MissingOneProperty<Model>


          which is, by the definition of MissingOneProperty:



          Model | {[K in keyof Model]: Pick<Model, Exclude<keyof Model, K>>}[keyof Model]


          which is, by mapping over the 'a', 'b', and 'c' properties of Model:



          Model | {
          a: Pick<Model, Exclude<keyof Model, 'a'>,
          b: Pick<Model, Exclude<keyof Model, 'b'>,
          c: Pick<Model, Exclude<keyof Model, 'c'>
          }[keyof Model]


          which is, by noting that keyof Model is 'a'|'b'|'c' and that Exclude<T, U> is a conditional type that removes elements from unions:



          Model | {
          a: Pick<Model, 'b'|'c'>,
          b: Pick<Model, 'a'|'c'>,
          c: Pick<Model, 'a'|'b'>
          }['a'|'b'|'c']


          which, by noting how Pick<> works, becomes:



          Model | {
          a: { b: number, c: number },
          b: { a: number, c: number },
          c: { a: number, b: number }
          }['a'|'b'|'c']


          which, finally, by noting that looking up a union of property keys in a type is the same as the union of the types of the properties, and by the definition of Model, turns into:



          {a: number, b: number, c: number} 
          | { b: number, c: number }
          | { a: number, c: number }
          | { a: number, b: number }


          Done! You can see how you end up with a union of Model and all ways of removing one property from Model.



          Hope that gives you some direction. Good luck!






          share|improve this answer


























          • The verbosity was in creating ModelWithoutA, ModelWithoutB, etc. types. Not just in using Pick. This is neat, but I can't quite piece together what MissingOneProperty<> does.

            – ABMagil
            Nov 19 '18 at 1:18






          • 1





            Edited: tried explaining it a bit by walking through how it gets evaluated.

            – jcalz
            Nov 19 '18 at 1:38














          2












          2








          2







          I'm not sure if the following counts as "incredibly verbose" (it uses Pick<> internally, so maybe?) or if it runs into the same compilation problems you saw, but:



          type MissingOneProperty<O extends object> = {
          [K in keyof O]: Pick<O, Exclude<keyof O, K>>
          }[keyof O];
          type MissingAtMostOneProperty<O extends object> =
          O | MissingOneProperty<O>;


          The idea is that MissingAtMostOneProperty<O> is either O or it is missing exactly one property from O. This probably only works for object types without index signatures (do you care?).



          So if I define your model like this:



          interface Model {
          a: number,
          b: number,
          c: number
          }


          I can declare a function that only accepts models missing at most one property:



          declare function solveModel(
          solvableModel: MissingAtMostOneProperty<Model>
          ): Model;
          solveModel({ a: 1, b: 2 }); // okay
          solveModel({ b: 2, c: 0.5 }); // okay
          solveModel({ a: 1, c: 0.5 }); // okay
          solveModel({ a: 1, b: 2, c: 0.5 }); // okay
          solveModel({ a: 1 }); // error, property "b" is missing
          solveModel({ b: 2 }); // error, property "a" is missing
          solveModel({ c: 0.5 }); // error, property "a" is missing
          solveModel({}); // error, property "a" is missing


          Looks reasonable to me.





          To understand how that works, let's walk through what MissingAtMostOneProperty<Model> gets evaluated to:



          MissingAtMostOneProperty<Model> 


          becomes, by the definition of MissingAtMostOneProperty:



          Model | MissingOneProperty<Model>


          which is, by the definition of MissingOneProperty:



          Model | {[K in keyof Model]: Pick<Model, Exclude<keyof Model, K>>}[keyof Model]


          which is, by mapping over the 'a', 'b', and 'c' properties of Model:



          Model | {
          a: Pick<Model, Exclude<keyof Model, 'a'>,
          b: Pick<Model, Exclude<keyof Model, 'b'>,
          c: Pick<Model, Exclude<keyof Model, 'c'>
          }[keyof Model]


          which is, by noting that keyof Model is 'a'|'b'|'c' and that Exclude<T, U> is a conditional type that removes elements from unions:



          Model | {
          a: Pick<Model, 'b'|'c'>,
          b: Pick<Model, 'a'|'c'>,
          c: Pick<Model, 'a'|'b'>
          }['a'|'b'|'c']


          which, by noting how Pick<> works, becomes:



          Model | {
          a: { b: number, c: number },
          b: { a: number, c: number },
          c: { a: number, b: number }
          }['a'|'b'|'c']


          which, finally, by noting that looking up a union of property keys in a type is the same as the union of the types of the properties, and by the definition of Model, turns into:



          {a: number, b: number, c: number} 
          | { b: number, c: number }
          | { a: number, c: number }
          | { a: number, b: number }


          Done! You can see how you end up with a union of Model and all ways of removing one property from Model.



          Hope that gives you some direction. Good luck!






          share|improve this answer















          I'm not sure if the following counts as "incredibly verbose" (it uses Pick<> internally, so maybe?) or if it runs into the same compilation problems you saw, but:



          type MissingOneProperty<O extends object> = {
          [K in keyof O]: Pick<O, Exclude<keyof O, K>>
          }[keyof O];
          type MissingAtMostOneProperty<O extends object> =
          O | MissingOneProperty<O>;


          The idea is that MissingAtMostOneProperty<O> is either O or it is missing exactly one property from O. This probably only works for object types without index signatures (do you care?).



          So if I define your model like this:



          interface Model {
          a: number,
          b: number,
          c: number
          }


          I can declare a function that only accepts models missing at most one property:



          declare function solveModel(
          solvableModel: MissingAtMostOneProperty<Model>
          ): Model;
          solveModel({ a: 1, b: 2 }); // okay
          solveModel({ b: 2, c: 0.5 }); // okay
          solveModel({ a: 1, c: 0.5 }); // okay
          solveModel({ a: 1, b: 2, c: 0.5 }); // okay
          solveModel({ a: 1 }); // error, property "b" is missing
          solveModel({ b: 2 }); // error, property "a" is missing
          solveModel({ c: 0.5 }); // error, property "a" is missing
          solveModel({}); // error, property "a" is missing


          Looks reasonable to me.





          To understand how that works, let's walk through what MissingAtMostOneProperty<Model> gets evaluated to:



          MissingAtMostOneProperty<Model> 


          becomes, by the definition of MissingAtMostOneProperty:



          Model | MissingOneProperty<Model>


          which is, by the definition of MissingOneProperty:



          Model | {[K in keyof Model]: Pick<Model, Exclude<keyof Model, K>>}[keyof Model]


          which is, by mapping over the 'a', 'b', and 'c' properties of Model:



          Model | {
          a: Pick<Model, Exclude<keyof Model, 'a'>,
          b: Pick<Model, Exclude<keyof Model, 'b'>,
          c: Pick<Model, Exclude<keyof Model, 'c'>
          }[keyof Model]


          which is, by noting that keyof Model is 'a'|'b'|'c' and that Exclude<T, U> is a conditional type that removes elements from unions:



          Model | {
          a: Pick<Model, 'b'|'c'>,
          b: Pick<Model, 'a'|'c'>,
          c: Pick<Model, 'a'|'b'>
          }['a'|'b'|'c']


          which, by noting how Pick<> works, becomes:



          Model | {
          a: { b: number, c: number },
          b: { a: number, c: number },
          c: { a: number, b: number }
          }['a'|'b'|'c']


          which, finally, by noting that looking up a union of property keys in a type is the same as the union of the types of the properties, and by the definition of Model, turns into:



          {a: number, b: number, c: number} 
          | { b: number, c: number }
          | { a: number, c: number }
          | { a: number, b: number }


          Done! You can see how you end up with a union of Model and all ways of removing one property from Model.



          Hope that gives you some direction. Good luck!







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 '18 at 1:37

























          answered Nov 18 '18 at 19:31









          jcalzjcalz

          23.9k22142




          23.9k22142













          • The verbosity was in creating ModelWithoutA, ModelWithoutB, etc. types. Not just in using Pick. This is neat, but I can't quite piece together what MissingOneProperty<> does.

            – ABMagil
            Nov 19 '18 at 1:18






          • 1





            Edited: tried explaining it a bit by walking through how it gets evaluated.

            – jcalz
            Nov 19 '18 at 1:38



















          • The verbosity was in creating ModelWithoutA, ModelWithoutB, etc. types. Not just in using Pick. This is neat, but I can't quite piece together what MissingOneProperty<> does.

            – ABMagil
            Nov 19 '18 at 1:18






          • 1





            Edited: tried explaining it a bit by walking through how it gets evaluated.

            – jcalz
            Nov 19 '18 at 1:38

















          The verbosity was in creating ModelWithoutA, ModelWithoutB, etc. types. Not just in using Pick. This is neat, but I can't quite piece together what MissingOneProperty<> does.

          – ABMagil
          Nov 19 '18 at 1:18





          The verbosity was in creating ModelWithoutA, ModelWithoutB, etc. types. Not just in using Pick. This is neat, but I can't quite piece together what MissingOneProperty<> does.

          – ABMagil
          Nov 19 '18 at 1:18




          1




          1





          Edited: tried explaining it a bit by walking through how it gets evaluated.

          – jcalz
          Nov 19 '18 at 1:38





          Edited: tried explaining it a bit by walking through how it gets evaluated.

          – jcalz
          Nov 19 '18 at 1:38


















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53364579%2fhow-to-model-interrelated-attributes-in-typescript%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







          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)