From 3f59f998adc235d6fbb51c24ae2ad0d838a199c8 Mon Sep 17 00:00:00 2001 From: Anthony Master Date: Fri, 6 Aug 2021 12:21:11 -0500 Subject: [PATCH] add types to useCookie hook added strict types fixes #291 with help and reference of https://github.com/microsoft/TypeScript/issues/20965#issuecomment-370114910 --- packages/react-cookie/src/useCookies.tsx | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/react-cookie/src/useCookies.tsx b/packages/react-cookie/src/useCookies.tsx index 83fe9bb..c01ddff 100644 --- a/packages/react-cookie/src/useCookies.tsx +++ b/packages/react-cookie/src/useCookies.tsx @@ -2,12 +2,12 @@ import { useContext, useEffect, useState, useRef, useMemo } from 'react'; import { Cookie, CookieSetOptions } from 'universal-cookie'; import CookiesContext from './CookiesContext'; -export default function useCookies( - dependencies?: string[] +export default function useCookies( + dependencies?: T[] ): [ - { [name: string]: any }, - (name: string, value: Cookie, options?: CookieSetOptions) => void, - (name: string, options?: CookieSetOptions) => void + U, + (name: T, value: Cookie, options?: CookieSetOptions) => void, + (name: T, options?: CookieSetOptions) => void ] { const cookies = useContext(CookiesContext); if (!cookies) { @@ -23,7 +23,7 @@ export default function useCookies( const newCookies = cookies.getAll(); if ( - shouldUpdate( + shouldUpdate( dependencies || null, newCookies, previousCookiesRef.current @@ -48,10 +48,10 @@ export default function useCookies( return [allCookies, setCookie, removeCookie]; } -function shouldUpdate( - dependencies: string[] | null, - newCookies: { [name: string]: any }, - oldCookies: { [name: string]: any } +function shouldUpdate( + dependencies: T[] | null, + newCookies: U, + oldCookies: U ) { if (!dependencies) { return true;