fix(nuxt): useCookie with defaults should return non-null value (#9449)

This commit is contained in:
Daniel Roe 2022-12-02 09:45:33 +00:00 committed by GitHub
parent 3501fd5ca7
commit c26979a047
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 5 deletions

View File

@ -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) || {}

View File

@ -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>>()