Skip to content

Instantly share code, notes, and snippets.

@MiKolka7

MiKolka7/.ts Secret

Last active December 10, 2019 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MiKolka7/d6db11024f4106e888a47cc7b3d16c3f to your computer and use it in GitHub Desktop.
Save MiKolka7/d6db11024f4106e888a47cc7b3d16c3f to your computer and use it in GitHub Desktop.
Declare interface Vuex commit without undefined params
import { CommitOptions } from 'vuex'
export declare type OmitByValue<T, ValueType> = Pick<T, {
[Key in keyof T]: T[Key] extends ValueType ? never : Key;
}[keyof T]>;
export declare type PickByValue<T, ValueType> = Pick<T, {
[Key in keyof T]: T[Key] extends ValueType ? Key : never;
}[keyof T]>;
type User = {
id: number
}
type MutationTypes = {
COMMON_INIT_USER: User
COMMON_CLEAR: undefined
}
type MutationWithParams = OmitByValue<MutationTypes, undefined>
type MutationWithoutParams = PickByValue<MutationTypes, undefined>
interface Commit {
<Key extends keyof MutationWithParams>(
name: Key,
payload: MutationTypes[Key],
options?: CommitOptions,
): void
<Key extends keyof MutationWithoutParams>(
name: Key,
payload?: undefined,
options?: CommitOptions,
): void
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment