stating that a property is not only “keyof” of a type but also an array of somethingKeyof that is also of type TClass extended from built-in Array in Typescript 1.6.2 does not update length while using [] operatorHow to type nested properties with keyof?Travel in PreOrder on a BinTree in typescriptTypescript recursively mapping object property types: object and array element typesKeyof that is also of type TWhy Vuex state return array as an objectRemove properties of a type from another typeFind in array with different typesTypescript - Ensure Generic Property Exists On Generic Type With Descriptive ErrorAllow arbitrary properties in class Typescript types

Why does Carol not get rid of the Kree symbol on her suit when she changes its colours?

"It doesn't matter" or "it won't matter"?

Review your own paper in Mathematics

A variation to the phrase "hanging over my shoulders"

How do you make your own symbol when Detexify fails?

Does the reader need to like the PoV character?

Has any country ever had 2 former presidents in jail simultaneously?

Multiplicative persistence

What does "Scientists rise up against statistical significance" mean? (Comment in Nature)

Why does the Sun have different day lengths, but not the gas giants?

How can ping know if my host is down

How to preserve electronics (computers, iPads and phones) for hundreds of years

Were Persian-Median kings illiterate?

Make a Bowl of Alphabet Soup

What are some good ways to treat frozen vegetables such that they behave like fresh vegetables when stir frying them?

Is there a RAID 0 Equivalent for RAM?

Does the Linux kernel need a file system to run?

How to convince somebody that he is fit for something else, but not this job?

How can I write humor as character trait?

15% tax on $7.5k earnings. Is that right?

Mimic lecturing on blackboard, facing audience

What is the English pronunciation of "pain au chocolat"?

Non-trope happy ending?

The IT department bottlenecks progress, how should I handle this?



stating that a property is not only “keyof” of a type but also an array of something


Keyof that is also of type TClass extended from built-in Array in Typescript 1.6.2 does not update length while using [] operatorHow to type nested properties with keyof?Travel in PreOrder on a BinTree in typescriptTypescript recursively mapping object property types: object and array element typesKeyof that is also of type TWhy Vuex state return array as an objectRemove properties of a type from another typeFind in array with different typesTypescript - Ensure Generic Property Exists On Generic Type With Descriptive ErrorAllow arbitrary properties in class Typescript types













0















I have the following function:



export function listMutations<S, M extends Model>(postfix: keyof S): MutationTree<S> 
return
ADD(state, payload)
state[postfix].push(payload.value)
,
CHANGED(state, payload) ,
REMOVED(state, payload) ,
RELATIONSHIP_ADDED(state, payload) ,
RELATIONSHIP_REMOVED(state, payload) ,




Where S represents a particular "state tree" and M is a given data model. The "model" is meant to fit into the state tree. For example, imagine that the state tree looked like this:



export interface StateTree<M> 
all: M[],
byId: ...



the postfix value passed into the function determines whether the property is "all" or something else but it is assumed that -- given the postfix property -- that:




  1. postfix is a keyof of the state tree

  2. and the value of the postfix property is an array of M

I have typed for the first condition but because this typing doesn't tell the type system that the value is always an array I get a type error on the .push() call.



I then looked at this question for reference: keyof-that-is-also-of-type-t. This helped somewhat and led to this attempt:



export type ListPropertyCandidates<T> = Pick<T, [K in keyof T]: T[K] extends Model[] ? K : never [keyof T]>

export function listMutations<T>(postfix: keyof ListPropertyCandidates<T>): MutationTree<T>
return
ADD(state, payload)
state[postfix].push(payload.value)
,
CHANGED(state, payload) ,
REMOVED(state, payload) ,
RELATIONSHIP_ADDED(state, payload) ,
RELATIONSHIP_REMOVED(state, payload) ,




I'd thought that inclusion of T[K] extends Model[] in the ListPropertyCandidates might get me there but the typing error persists in that it doesn't recognize the state[postfix] as always being an array.



Anyone have any ideas?










share|improve this question
























  • You refer it as prefix outside the code, but name it postfix inside the code, are they supposed to be the same?

    – paibamboo
    Mar 7 at 7:35











  • If I understand you correctly, you could define the type of state like so ADD<V>(state: postfix: V[], payload: value: V) state.postfix.push(payload.value);

    – paibamboo
    Mar 7 at 7:42












  • Sorry it should say postfix everywhere; will update

    – ken
    Mar 7 at 19:50















0















I have the following function:



export function listMutations<S, M extends Model>(postfix: keyof S): MutationTree<S> 
return
ADD(state, payload)
state[postfix].push(payload.value)
,
CHANGED(state, payload) ,
REMOVED(state, payload) ,
RELATIONSHIP_ADDED(state, payload) ,
RELATIONSHIP_REMOVED(state, payload) ,




Where S represents a particular "state tree" and M is a given data model. The "model" is meant to fit into the state tree. For example, imagine that the state tree looked like this:



export interface StateTree<M> 
all: M[],
byId: ...



the postfix value passed into the function determines whether the property is "all" or something else but it is assumed that -- given the postfix property -- that:




  1. postfix is a keyof of the state tree

  2. and the value of the postfix property is an array of M

I have typed for the first condition but because this typing doesn't tell the type system that the value is always an array I get a type error on the .push() call.



I then looked at this question for reference: keyof-that-is-also-of-type-t. This helped somewhat and led to this attempt:



export type ListPropertyCandidates<T> = Pick<T, [K in keyof T]: T[K] extends Model[] ? K : never [keyof T]>

export function listMutations<T>(postfix: keyof ListPropertyCandidates<T>): MutationTree<T>
return
ADD(state, payload)
state[postfix].push(payload.value)
,
CHANGED(state, payload) ,
REMOVED(state, payload) ,
RELATIONSHIP_ADDED(state, payload) ,
RELATIONSHIP_REMOVED(state, payload) ,




I'd thought that inclusion of T[K] extends Model[] in the ListPropertyCandidates might get me there but the typing error persists in that it doesn't recognize the state[postfix] as always being an array.



Anyone have any ideas?










share|improve this question
























  • You refer it as prefix outside the code, but name it postfix inside the code, are they supposed to be the same?

    – paibamboo
    Mar 7 at 7:35











  • If I understand you correctly, you could define the type of state like so ADD<V>(state: postfix: V[], payload: value: V) state.postfix.push(payload.value);

    – paibamboo
    Mar 7 at 7:42












  • Sorry it should say postfix everywhere; will update

    – ken
    Mar 7 at 19:50













0












0








0








I have the following function:



export function listMutations<S, M extends Model>(postfix: keyof S): MutationTree<S> 
return
ADD(state, payload)
state[postfix].push(payload.value)
,
CHANGED(state, payload) ,
REMOVED(state, payload) ,
RELATIONSHIP_ADDED(state, payload) ,
RELATIONSHIP_REMOVED(state, payload) ,




Where S represents a particular "state tree" and M is a given data model. The "model" is meant to fit into the state tree. For example, imagine that the state tree looked like this:



export interface StateTree<M> 
all: M[],
byId: ...



the postfix value passed into the function determines whether the property is "all" or something else but it is assumed that -- given the postfix property -- that:




  1. postfix is a keyof of the state tree

  2. and the value of the postfix property is an array of M

I have typed for the first condition but because this typing doesn't tell the type system that the value is always an array I get a type error on the .push() call.



I then looked at this question for reference: keyof-that-is-also-of-type-t. This helped somewhat and led to this attempt:



export type ListPropertyCandidates<T> = Pick<T, [K in keyof T]: T[K] extends Model[] ? K : never [keyof T]>

export function listMutations<T>(postfix: keyof ListPropertyCandidates<T>): MutationTree<T>
return
ADD(state, payload)
state[postfix].push(payload.value)
,
CHANGED(state, payload) ,
REMOVED(state, payload) ,
RELATIONSHIP_ADDED(state, payload) ,
RELATIONSHIP_REMOVED(state, payload) ,




I'd thought that inclusion of T[K] extends Model[] in the ListPropertyCandidates might get me there but the typing error persists in that it doesn't recognize the state[postfix] as always being an array.



Anyone have any ideas?










share|improve this question
















I have the following function:



export function listMutations<S, M extends Model>(postfix: keyof S): MutationTree<S> 
return
ADD(state, payload)
state[postfix].push(payload.value)
,
CHANGED(state, payload) ,
REMOVED(state, payload) ,
RELATIONSHIP_ADDED(state, payload) ,
RELATIONSHIP_REMOVED(state, payload) ,




Where S represents a particular "state tree" and M is a given data model. The "model" is meant to fit into the state tree. For example, imagine that the state tree looked like this:



export interface StateTree<M> 
all: M[],
byId: ...



the postfix value passed into the function determines whether the property is "all" or something else but it is assumed that -- given the postfix property -- that:




  1. postfix is a keyof of the state tree

  2. and the value of the postfix property is an array of M

I have typed for the first condition but because this typing doesn't tell the type system that the value is always an array I get a type error on the .push() call.



I then looked at this question for reference: keyof-that-is-also-of-type-t. This helped somewhat and led to this attempt:



export type ListPropertyCandidates<T> = Pick<T, [K in keyof T]: T[K] extends Model[] ? K : never [keyof T]>

export function listMutations<T>(postfix: keyof ListPropertyCandidates<T>): MutationTree<T>
return
ADD(state, payload)
state[postfix].push(payload.value)
,
CHANGED(state, payload) ,
REMOVED(state, payload) ,
RELATIONSHIP_ADDED(state, payload) ,
RELATIONSHIP_REMOVED(state, payload) ,




I'd thought that inclusion of T[K] extends Model[] in the ListPropertyCandidates might get me there but the typing error persists in that it doesn't recognize the state[postfix] as always being an array.



Anyone have any ideas?







typescript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 19:51







ken

















asked Mar 7 at 5:05









kenken

3,63974289




3,63974289












  • You refer it as prefix outside the code, but name it postfix inside the code, are they supposed to be the same?

    – paibamboo
    Mar 7 at 7:35











  • If I understand you correctly, you could define the type of state like so ADD<V>(state: postfix: V[], payload: value: V) state.postfix.push(payload.value);

    – paibamboo
    Mar 7 at 7:42












  • Sorry it should say postfix everywhere; will update

    – ken
    Mar 7 at 19:50

















  • You refer it as prefix outside the code, but name it postfix inside the code, are they supposed to be the same?

    – paibamboo
    Mar 7 at 7:35











  • If I understand you correctly, you could define the type of state like so ADD<V>(state: postfix: V[], payload: value: V) state.postfix.push(payload.value);

    – paibamboo
    Mar 7 at 7:42












  • Sorry it should say postfix everywhere; will update

    – ken
    Mar 7 at 19:50
















You refer it as prefix outside the code, but name it postfix inside the code, are they supposed to be the same?

– paibamboo
Mar 7 at 7:35





You refer it as prefix outside the code, but name it postfix inside the code, are they supposed to be the same?

– paibamboo
Mar 7 at 7:35













If I understand you correctly, you could define the type of state like so ADD<V>(state: postfix: V[], payload: value: V) state.postfix.push(payload.value);

– paibamboo
Mar 7 at 7:42






If I understand you correctly, you could define the type of state like so ADD<V>(state: postfix: V[], payload: value: V) state.postfix.push(payload.value);

– paibamboo
Mar 7 at 7:42














Sorry it should say postfix everywhere; will update

– ken
Mar 7 at 19:50





Sorry it should say postfix everywhere; will update

– ken
Mar 7 at 19:50












1 Answer
1






active

oldest

votes


















0














I don't think it's possible without defining the type of state itself



function listMutations<S, M extends Model>(postfix: keyof S): MutationTree<S> 
return
ADD(state: Record<keyof S, M[]>, payload: value: M)
state[postfix].push(payload.value);
,
...







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%2f55036450%2fstating-that-a-property-is-not-only-keyof-of-a-type-but-also-an-array-of-somet%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









    0














    I don't think it's possible without defining the type of state itself



    function listMutations<S, M extends Model>(postfix: keyof S): MutationTree<S> 
    return
    ADD(state: Record<keyof S, M[]>, payload: value: M)
    state[postfix].push(payload.value);
    ,
    ...







    share|improve this answer



























      0














      I don't think it's possible without defining the type of state itself



      function listMutations<S, M extends Model>(postfix: keyof S): MutationTree<S> 
      return
      ADD(state: Record<keyof S, M[]>, payload: value: M)
      state[postfix].push(payload.value);
      ,
      ...







      share|improve this answer

























        0












        0








        0







        I don't think it's possible without defining the type of state itself



        function listMutations<S, M extends Model>(postfix: keyof S): MutationTree<S> 
        return
        ADD(state: Record<keyof S, M[]>, payload: value: M)
        state[postfix].push(payload.value);
        ,
        ...







        share|improve this answer













        I don't think it's possible without defining the type of state itself



        function listMutations<S, M extends Model>(postfix: keyof S): MutationTree<S> 
        return
        ADD(state: Record<keyof S, M[]>, payload: value: M)
        state[postfix].push(payload.value);
        ,
        ...








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 8 at 1:52









        paibamboopaibamboo

        1,5311113




        1,5311113





























            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%2f55036450%2fstating-that-a-property-is-not-only-keyof-of-a-type-but-also-an-array-of-somet%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

            Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

            Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

            Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved