From 0c4e3c61e66a7d12990897e7d0b0cd9438efdb88 Mon Sep 17 00:00:00 2001 From: Micah Engle-Eshleman Date: Mon, 1 Feb 2021 10:50:27 -0800 Subject: [PATCH] Make ContextDetail.unsubscribe property optional Fixes Typescript compilation error due to unsubscribe property not existing on ContextDetail when a context is used without a provider. --- src/create-context.ts | 3 ++- src/use-context.ts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/create-context.ts b/src/create-context.ts index ed0169f..5a624a1 100644 --- a/src/create-context.ts +++ b/src/create-context.ts @@ -20,8 +20,9 @@ interface ContextDetail { Context: Context; callback: (value: T) => void; + // These properties will not exist if a context consumer lacks a provider value: T; - unsubscribe: (this: Context) => void; + unsubscribe?: (this: Context) => void; } function makeContext(component: ComponentCreator): Creator { diff --git a/src/use-context.ts b/src/use-context.ts index df3bc88..53be227 100644 --- a/src/use-context.ts +++ b/src/use-context.ts @@ -55,7 +55,7 @@ const useContext = hook(class extends Hook<[Context], T, Element> { composed: true, // to pass ShadowDOM boundaries })); - const { unsubscribe, value } = detail as ContextDetail; + const { unsubscribe = null, value } = detail as ContextDetail; this.value = unsubscribe ? value : Context.defaultValue;