flekschas/svelte-simple-modal

Uncaught (in promise) TypeError: Cannot destructure property 'open' of 'getContext(...)' as it is undefined.

impossibletower opened this issue · 2 comments

I am getting this error: Uncaught (in promise) TypeError: Cannot destructure property 'open' of 'getContext(...)' as it is undefined.

The code runs fine even though it's throwing that error. Cannot for the life of me figure out why it's throwing it.

I followed the instructions where it's inside a wrapper, and use import { getContext } from 'svelte'; const { open, close } = getContext('simple-modal');

I resolved mine by adding a generic type to getContext

import type { Modal } from 'svelte-simple-modal';

const { open, close } = getContext<Modal>('simple-modal');

Yeah, getContext doesn't automatically know what the context type is so you have to specify it as @KuyaKoya showed.

You can also use the special Context type as it defines only the two exposed functions:

import type { Context } from 'svelte-simple-modal';

const { open, close } = getContext<Context>('simple-modal');