Define a list of optional keys for Typescript Record












2














I want to type an object which can only have keys 'a', 'b' or 'c'.



So I can do it as follows:



Interface IList {
a?: string;
b?: string;
c?: string;
}


They are all optional!
Now I was wondering if this can be written with Record in just one line



type List = Record<'a' | 'b' | 'c', string>;


The only issue is that all keys need to be defined. So I ended up with



type List = Partial<Record<'a' | 'b' | 'c', string>>;


This works, but I can imagine there is a better way to do this without Partial. Is there an other way to make the keys optional inside Record ?










share|improve this question





























    2














    I want to type an object which can only have keys 'a', 'b' or 'c'.



    So I can do it as follows:



    Interface IList {
    a?: string;
    b?: string;
    c?: string;
    }


    They are all optional!
    Now I was wondering if this can be written with Record in just one line



    type List = Record<'a' | 'b' | 'c', string>;


    The only issue is that all keys need to be defined. So I ended up with



    type List = Partial<Record<'a' | 'b' | 'c', string>>;


    This works, but I can imagine there is a better way to do this without Partial. Is there an other way to make the keys optional inside Record ?










    share|improve this question



























      2












      2








      2







      I want to type an object which can only have keys 'a', 'b' or 'c'.



      So I can do it as follows:



      Interface IList {
      a?: string;
      b?: string;
      c?: string;
      }


      They are all optional!
      Now I was wondering if this can be written with Record in just one line



      type List = Record<'a' | 'b' | 'c', string>;


      The only issue is that all keys need to be defined. So I ended up with



      type List = Partial<Record<'a' | 'b' | 'c', string>>;


      This works, but I can imagine there is a better way to do this without Partial. Is there an other way to make the keys optional inside Record ?










      share|improve this question















      I want to type an object which can only have keys 'a', 'b' or 'c'.



      So I can do it as follows:



      Interface IList {
      a?: string;
      b?: string;
      c?: string;
      }


      They are all optional!
      Now I was wondering if this can be written with Record in just one line



      type List = Record<'a' | 'b' | 'c', string>;


      The only issue is that all keys need to be defined. So I ended up with



      type List = Partial<Record<'a' | 'b' | 'c', string>>;


      This works, but I can imagine there is a better way to do this without Partial. Is there an other way to make the keys optional inside Record ?







      typescript typescript-typings






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 9:18







      Jeanluca Scaljeri

















      asked Nov 13 '18 at 8:30









      Jeanluca ScaljeriJeanluca Scaljeri

      6,96323104178




      6,96323104178
























          2 Answers
          2






          active

          oldest

          votes


















          4














          There is no way to specify the optionality of members of Record. They are required by definition



          type Record<K extends keyof any, T> = {
          [P in K]: T; // Mapped properties are not optional, and it's not a homomorphic mapped type so it can't come from anywhere else.
          };


          You can define your own type if this is a common scenario for you:



          type PartialRecord<K extends keyof any, T> = {
          [P in K]?: T;
          };
          type List = PartialRecord<'a' | 'b' | 'c', string>


          Or you can define PartialRecord using the predefined mapped types as well:



          type PartialRecord<K extends keyof any, T> =  Partial<Record<K, T>>





          share|improve this answer

















          • 1




            This is a good comprehensive answer.
            – Fenton
            Nov 13 '18 at 8:37



















          3














          You can create the partial version of your List type:



          type PartialList = Partial<List>;


          And you could do it all on one line if you don't want the intermediate type:



          type PartialList = Partial<Record<'a' | 'b' | 'c', string>>;


          You might decide that, in the end, the most expressive for your future self is:



          type List = {
          a?: string;
          b?: string;
          c?: string;
          }





          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',
            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%2f53276792%2fdefine-a-list-of-optional-keys-for-typescript-record%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            4














            There is no way to specify the optionality of members of Record. They are required by definition



            type Record<K extends keyof any, T> = {
            [P in K]: T; // Mapped properties are not optional, and it's not a homomorphic mapped type so it can't come from anywhere else.
            };


            You can define your own type if this is a common scenario for you:



            type PartialRecord<K extends keyof any, T> = {
            [P in K]?: T;
            };
            type List = PartialRecord<'a' | 'b' | 'c', string>


            Or you can define PartialRecord using the predefined mapped types as well:



            type PartialRecord<K extends keyof any, T> =  Partial<Record<K, T>>





            share|improve this answer

















            • 1




              This is a good comprehensive answer.
              – Fenton
              Nov 13 '18 at 8:37
















            4














            There is no way to specify the optionality of members of Record. They are required by definition



            type Record<K extends keyof any, T> = {
            [P in K]: T; // Mapped properties are not optional, and it's not a homomorphic mapped type so it can't come from anywhere else.
            };


            You can define your own type if this is a common scenario for you:



            type PartialRecord<K extends keyof any, T> = {
            [P in K]?: T;
            };
            type List = PartialRecord<'a' | 'b' | 'c', string>


            Or you can define PartialRecord using the predefined mapped types as well:



            type PartialRecord<K extends keyof any, T> =  Partial<Record<K, T>>





            share|improve this answer

















            • 1




              This is a good comprehensive answer.
              – Fenton
              Nov 13 '18 at 8:37














            4












            4








            4






            There is no way to specify the optionality of members of Record. They are required by definition



            type Record<K extends keyof any, T> = {
            [P in K]: T; // Mapped properties are not optional, and it's not a homomorphic mapped type so it can't come from anywhere else.
            };


            You can define your own type if this is a common scenario for you:



            type PartialRecord<K extends keyof any, T> = {
            [P in K]?: T;
            };
            type List = PartialRecord<'a' | 'b' | 'c', string>


            Or you can define PartialRecord using the predefined mapped types as well:



            type PartialRecord<K extends keyof any, T> =  Partial<Record<K, T>>





            share|improve this answer












            There is no way to specify the optionality of members of Record. They are required by definition



            type Record<K extends keyof any, T> = {
            [P in K]: T; // Mapped properties are not optional, and it's not a homomorphic mapped type so it can't come from anywhere else.
            };


            You can define your own type if this is a common scenario for you:



            type PartialRecord<K extends keyof any, T> = {
            [P in K]?: T;
            };
            type List = PartialRecord<'a' | 'b' | 'c', string>


            Or you can define PartialRecord using the predefined mapped types as well:



            type PartialRecord<K extends keyof any, T> =  Partial<Record<K, T>>






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 '18 at 8:36









            Titian Cernicova-DragomirTitian Cernicova-Dragomir

            58.4k33552




            58.4k33552








            • 1




              This is a good comprehensive answer.
              – Fenton
              Nov 13 '18 at 8:37














            • 1




              This is a good comprehensive answer.
              – Fenton
              Nov 13 '18 at 8:37








            1




            1




            This is a good comprehensive answer.
            – Fenton
            Nov 13 '18 at 8:37




            This is a good comprehensive answer.
            – Fenton
            Nov 13 '18 at 8:37













            3














            You can create the partial version of your List type:



            type PartialList = Partial<List>;


            And you could do it all on one line if you don't want the intermediate type:



            type PartialList = Partial<Record<'a' | 'b' | 'c', string>>;


            You might decide that, in the end, the most expressive for your future self is:



            type List = {
            a?: string;
            b?: string;
            c?: string;
            }





            share|improve this answer


























              3














              You can create the partial version of your List type:



              type PartialList = Partial<List>;


              And you could do it all on one line if you don't want the intermediate type:



              type PartialList = Partial<Record<'a' | 'b' | 'c', string>>;


              You might decide that, in the end, the most expressive for your future self is:



              type List = {
              a?: string;
              b?: string;
              c?: string;
              }





              share|improve this answer
























                3












                3








                3






                You can create the partial version of your List type:



                type PartialList = Partial<List>;


                And you could do it all on one line if you don't want the intermediate type:



                type PartialList = Partial<Record<'a' | 'b' | 'c', string>>;


                You might decide that, in the end, the most expressive for your future self is:



                type List = {
                a?: string;
                b?: string;
                c?: string;
                }





                share|improve this answer












                You can create the partial version of your List type:



                type PartialList = Partial<List>;


                And you could do it all on one line if you don't want the intermediate type:



                type PartialList = Partial<Record<'a' | 'b' | 'c', string>>;


                You might decide that, in the end, the most expressive for your future self is:



                type List = {
                a?: string;
                b?: string;
                c?: string;
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 8:36









                FentonFenton

                152k42287311




                152k42287311






























                    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%2f53276792%2fdefine-a-list-of-optional-keys-for-typescript-record%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

                    List item for chat from Array inside array React Native

                    Thiostrepton

                    Caerphilly