Skip to content

Commit

Permalink
fix(use-v-model): missed prefix types
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard Aksamitov committed Dec 8, 2021
1 parent 89deade commit 8ebdacd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
12 changes: 10 additions & 2 deletions use-v-model/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@ let mapModel = useVModel(mapStore)
let mapKeyState = useVModel(mapStore, 'letter')
// THROWS not assignable to parameter
let mapKeysState = useVModel(mapStore, ['letter', 'number'], { test: 'a' })
let mapKeysStatePrefixed = useVModel(mapStore, ['letter', 'number'], {
prefix: 'Test'
})

// THROWS does not exist
mapModel.value.other = 'b'
// THROWS not assignable to type 'string'
mapKeyState.value = 1

// THROWS not assignable to type 'string'
mapKeysState.letter.value = 0
mapKeysState.letterModel.value = 0
// THROWS does not exist
mapKeysState.other.value
mapKeysState.letter.value
// THROWS does not exist
mapKeysState.otherModel.value

// THROWS does not exist
mapKeysStatePrefixed.letter.value
11 changes: 6 additions & 5 deletions use-v-model/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Ref } from 'vue'

type UnwarpKeys<Keys> = Keys extends (infer Key)[] ? Key : Keys

interface UseVModelOptions {
prefix: string
interface UseVModelOptions<Prefix extends string> {
prefix: Prefix
}

/**
Expand Down Expand Up @@ -37,15 +37,16 @@ interface UseVModelOptions {
export function useVModel<
SomeStore extends Store,
Value extends StoreValue<SomeStore>,
Keys extends keyof Value | (keyof Value)[] = undefined
Keys extends keyof Value | (keyof Value)[] = undefined,
Prefix extends string = 'Model'
>(
store: SomeStore,
keys?: Keys,
opts?: UseVModelOptions
opts?: UseVModelOptions<Prefix>
): Keys extends undefined
? Ref<Value>
: Keys extends (keyof Value)[]
? {
[Key in UnwarpKeys<Keys>]: Ref<Value[Key]>
[Key in UnwarpKeys<Keys> as `${Key}${Prefix}`]: Ref<Value[Key]>
}
: Ref<Value[Keys]>

0 comments on commit 8ebdacd

Please sign in to comment.