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
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:
postfix
is a keyof of the state tree- and the value of the
postfix
property is an array ofM
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
add a comment |
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:
postfix
is a keyof of the state tree- and the value of the
postfix
property is an array ofM
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
You refer it asprefix
outside the code, but name itpostfix
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 ofstate
like soADD<V>(state: postfix: V[], payload: value: V) state.postfix.push(payload.value);
– paibamboo
Mar 7 at 7:42
Sorry it should saypostfix
everywhere; will update
– ken
Mar 7 at 19:50
add a comment |
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:
postfix
is a keyof of the state tree- and the value of the
postfix
property is an array ofM
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
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:
postfix
is a keyof of the state tree- and the value of the
postfix
property is an array ofM
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
typescript
edited Mar 7 at 19:51
ken
asked Mar 7 at 5:05
kenken
3,63974289
3,63974289
You refer it asprefix
outside the code, but name itpostfix
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 ofstate
like soADD<V>(state: postfix: V[], payload: value: V) state.postfix.push(payload.value);
– paibamboo
Mar 7 at 7:42
Sorry it should saypostfix
everywhere; will update
– ken
Mar 7 at 19:50
add a comment |
You refer it asprefix
outside the code, but name itpostfix
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 ofstate
like soADD<V>(state: postfix: V[], payload: value: V) state.postfix.push(payload.value);
– paibamboo
Mar 7 at 7:42
Sorry it should saypostfix
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
add a comment |
1 Answer
1
active
oldest
votes
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);
,
...
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
,
...
add a comment |
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);
,
...
add a comment |
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);
,
...
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);
,
...
answered Mar 8 at 1:52
paibamboopaibamboo
1,5311113
1,5311113
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
You refer it as
prefix
outside the code, but name itpostfix
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 soADD<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