mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 23:22:02 +00:00
fix(nuxt): useCookie
with defaults should return non-null value (#9449)
This commit is contained in:
parent
3501fd5ca7
commit
c26979a047
@ -15,7 +15,7 @@ export interface CookieOptions<T = any> extends _CookieOptions {
|
|||||||
default?: () => T | Ref<T>
|
default?: () => T | Ref<T>
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CookieRef<T> extends Ref<T | null> {}
|
export interface CookieRef<T> extends Ref<T> {}
|
||||||
|
|
||||||
const CookieDefaults: CookieOptions<any> = {
|
const CookieDefaults: CookieOptions<any> = {
|
||||||
path: '/',
|
path: '/',
|
||||||
@ -23,7 +23,7 @@ const CookieDefaults: CookieOptions<any> = {
|
|||||||
encode: val => encodeURIComponent(typeof val === 'string' ? val : JSON.stringify(val))
|
encode: val => encodeURIComponent(typeof val === 'string' ? val : JSON.stringify(val))
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useCookie <T = string> (name: string, _opts?: CookieOptions<T>): CookieRef<T> {
|
export function useCookie <T = string | null> (name: string, _opts?: CookieOptions<T>): CookieRef<T> {
|
||||||
const opts = { ...CookieDefaults, ..._opts }
|
const opts = { ...CookieDefaults, ..._opts }
|
||||||
const cookies = readRawCookies(opts) || {}
|
const cookies = readRawCookies(opts) || {}
|
||||||
|
|
||||||
|
6
test/fixtures/basic/types.ts
vendored
6
test/fixtures/basic/types.ts
vendored
@ -161,9 +161,9 @@ describe('composables', () => {
|
|||||||
expectTypeOf(useState('test', () => ref('hello'))).toEqualTypeOf<Ref<string>>()
|
expectTypeOf(useState('test', () => ref('hello'))).toEqualTypeOf<Ref<string>>()
|
||||||
expectTypeOf(useState('test', () => 'hello')).toEqualTypeOf<Ref<string>>()
|
expectTypeOf(useState('test', () => 'hello')).toEqualTypeOf<Ref<string>>()
|
||||||
|
|
||||||
expectTypeOf(useCookie('test', { default: () => ref(500) })).toEqualTypeOf<Ref<number | null>>()
|
expectTypeOf(useCookie('test', { default: () => ref(500) })).toEqualTypeOf<Ref<number>>()
|
||||||
expectTypeOf(useCookie('test', { default: () => 500 })).toEqualTypeOf<Ref<number | null>>()
|
expectTypeOf(useCookie('test', { default: () => 500 })).toEqualTypeOf<Ref<number>>()
|
||||||
useCookie('test').value = null
|
useCookie<number | null>('test').value = null
|
||||||
|
|
||||||
expectTypeOf(useAsyncData('test', () => Promise.resolve(500), { default: () => ref(500) }).data).toEqualTypeOf<Ref<number | null>>()
|
expectTypeOf(useAsyncData('test', () => Promise.resolve(500), { default: () => ref(500) }).data).toEqualTypeOf<Ref<number | null>>()
|
||||||
expectTypeOf(useAsyncData('test', () => Promise.resolve(500), { default: () => 500 }).data).toEqualTypeOf<Ref<number | null>>()
|
expectTypeOf(useAsyncData('test', () => Promise.resolve(500), { default: () => 500 }).data).toEqualTypeOf<Ref<number | null>>()
|
||||||
|
Loading…
Reference in New Issue
Block a user