Skip to content

Commit

Permalink
fix: correctly type ssrRef with factory function
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed May 7, 2020
1 parent db266a5 commit 7b734ac
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/ssr-ref.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ref, Ref, onServerPrefetch as prefetch } from '@vue/composition-api'

function getValue(value: any) {
if (typeof value === 'function') return value()
function getValue<T>(value: T | (() => T)): T {
if (value instanceof Function) return value()
return value
}

Expand All @@ -24,7 +24,7 @@ export function injectRefs() {
})
}

export const ssrRef = <T>(value: T, key?: string) => {
export const ssrRef = <T>(value: T | (() => T), key?: string): Ref<T> => {
const val = ref<T>(getValue(value))

if (!key)
Expand All @@ -44,7 +44,7 @@ export const ssrRef = <T>(value: T, key?: string) => {
refs.push([key, val])
}

return val
return val as Ref<T>
}

export const onServerPrefetch = (callback: Function) => {
Expand Down
11 changes: 11 additions & 0 deletions test/tsd/ssr-ref.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { expectType } from 'tsd'

import { ssrRef, Ref } from '../..'

// eslint-disable-next-line
expectType<Ref<number>>(ssrRef(() => 42))
expectType<Ref<string>>(ssrRef('thoughtless'))
interface Obj {
name: string
}
expectType<Ref<Obj>>(ssrRef({ name: 'today' }))

0 comments on commit 7b734ac

Please sign in to comment.