Skip to content

Commit

Permalink
fix(useToogle): fix wrong type and ban changing state with ref
Browse files Browse the repository at this point in the history
  • Loading branch information
lmhcoding committed Sep 25, 2020
1 parent f5f039c commit 963aca2
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/useToggle.ts
@@ -1,23 +1,23 @@
import { ref, Ref } from 'vue'
import { ref, Ref, DeepReadonly, readonly } from 'vue'

export type ToggleParamType = string | number | boolean | undefined | null

export interface ToggleReturn<T extends ToggleParamType = ToggleParamType> {
state: Ref<T>
export interface ToggleReturn {
state: DeepReadonly<Ref<boolean>>
toggle: (next?: boolean) => void
setDefault: () => void
setRight: () => void
}

export interface ITwoType<T extends ToggleParamType = ToggleParamType, U extends ToggleParamType = ToggleParamType>
extends Omit<ToggleReturn<T>, 'state' | 'toggle'> {
state: Ref<T | U>
extends Omit<ToggleReturn, 'state' | 'toggle'> {
state: DeepReadonly<Ref<T | U>>
toggle: (next?: T | U) => void
}

export interface IToggle {
(): ToggleReturn<boolean>
<T extends ToggleParamType = ToggleParamType>(defaultValue: T): ToggleReturn<T>
(): ToggleReturn
(defaultValue: boolean): ToggleReturn
<T extends ToggleParamType = ToggleParamType, U extends ToggleParamType = ToggleParamType>(
defaultValue: T,
resetValue?: U
Expand All @@ -27,7 +27,7 @@ export interface IToggle {
export const useToggle: IToggle = <
T extends ToggleParamType = ToggleParamType,
U extends ToggleParamType = ToggleParamType
>(
> (
defaultValue: T = false as T,
reverseValue?: U
) => {
Expand All @@ -48,7 +48,7 @@ export const useToggle: IToggle = <
state.value = reverseTo
}
return {
state,
state: readonly(state),
setDefault,
setRight,
toggle
Expand Down

0 comments on commit 963aca2

Please sign in to comment.