Skip to content

Instantly share code, notes, and snippets.

View przemyslawjanpietrzak's full-sized avatar

Przemyslaw Jan Beigert przemyslawjanpietrzak

View GitHub Profile
@przemyslawjanpietrzak
przemyslawjanpietrzak / module1.mutations.ts
Created October 31, 2021 20:37
literal-types-vuex.ts
interface Module1Mutations {
mutationA1(state: Module1State, payload: MutationA1Payload): void
mutationA2(state: Module1State, payload: MutationA2Payload): void
}
export const module1Mutations: MutationTree<Module1State> & Module1Mutations = {
mutationA1(state, payload) {},
mutationA2(state, payload) {},
};
@przemyslawjanpietrzak
przemyslawjanpietrzak / module1.actions.ts
Created October 31, 2021 20:33
literal-types-vuex.ts
interface Module1Actions {
actionA1(context: Module1ActionContext, payload: ActionA1Payload): void
actionA2(context: Module1ActionContext, payload: ActionA2Payload): void
}
type Module1ActionContext = {
dispatch<K extends keyof Module1Actions>(
actionType: K,
payload: Parameters<Module1Actions[K]>[1],
options?: DispatchOptions,
type Color = "red" | "blue";
type Quantity = "one" | "two";
type SeussFish = `${Quantity | Color} fish`;
// same as
// type SeussFish = "one fish" | "two fish"
// | "red fish" | "blue fish";
@przemyslawjanpietrzak
przemyslawjanpietrzak / Group.vue
Created October 9, 2021 16:59
Improve $destroy performance in Vue #2
computed: {
mappedItems() {
return this.items.map(item => ({
...item,
a: getA(item),
b: getB(item),
c: getC(item),
d: getD(item),
e: getE(item),
});
@przemyslawjanpietrzak
przemyslawjanpietrzak / Group.vue
Created October 9, 2021 16:59
Improve $destroy performance in Vue #2
computed: {
mappedItems() {
return this.items.map(item => ({
...item,
a: getA(item),
b: getB(item),
c: getC(item),
d: getD(item),
e: getE(item),
});
@przemyslawjanpietrzak
przemyslawjanpietrzak / Item.vue
Created October 9, 2021 16:57
Improve $destroy performance in Vue #1
// Item.vue
...mapGetters('namespace', [
'getA',
'getB',
'getC',
'getD',
'getE',
});
let fn = (buttonSize: BUTTON_SIZES_UNION) => {};
fn('small');
fn('smal'); // Argument of type '"smal"' is not assignable to parameter of type 'BUTTON_SIZES_UNION'
let str: string;
fn(str); // Argument of type 'string' is not assignable to parameter of type 'BUTTON_SIZES_UNION'.
type BUTTON_SIZES_UNION = "small" | "medium" | "large";
const obj = { buttonSize: BUTTON_SIZES_CONST_ENUM } // ERROR: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
Object.values(BUTTON_SIZES_CONST_ENUM); // ERROR 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
const buttonSizeUsage = "medium" /* MEDIUM */;