Reputation: 9013
In Typescript I'd like to leverage my string enumeration:
export const enum MutationKeys {
registerUser = 'registration/REGISTER',
registerUserCompleted = 'registration/REGISTER_COMPLETED'
}
so that it's string values provide type-checking constraints on an object like this:
const mutations: IDictionary<VuexMutation> = {
['registration/REGISTER'](state, payload) {
state.registration = {
meta: {
serverValidated: false
},
value: payload
};
},
['registration/REGISTER_COMPLETED'](state) {
state.registration.meta.serverValidated = true;
}
};
In the above example the
IDictionary<VueMutation>
interface allows me to type the value of the object vlue but allows any string index.
Upvotes: 1
Views: 932
Reputation: 4240
You can use key in
construct :
export const enum MutationKeys {
registerUser = 'registration/REGISTER',
registerUserCompleted = 'registration/REGISTER_COMPLETED'
}
type MutationDictionnary<P> = {
[key in MutationKeys]: P
}
const mutations: MutationDictionnary<VuexMutation> = {
['registration/REGISTER'](state, payload) {
state.registration = {
meta: {
serverValidated: false
},
value: payload
};
},
['registration/REGISTER_COMPLETED'](state) {
state.registration.meta.serverValidated = true;
}
};
Upvotes: 4