Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property 'close' does not exist on type 'unknown' #88

Open
KoljaL opened this issue Nov 8, 2022 · 8 comments
Open

Property 'close' does not exist on type 'unknown' #88

KoljaL opened this issue Nov 8, 2022 · 8 comments
Labels
improvement Improved feature

Comments

@KoljaL
Copy link

KoljaL commented Nov 8, 2022

My Login.svelteModal starts like this:

<script lang="ts">
	import { getContext } from 'svelte';
	const { close } = getContext('simple-modal');

	export let title: string;
	export let callbackFCN = (value: boolean) => {};
    ....

    if (login === true) { 
        close();
        callbackFCN(true);
    } else {
        errorMsg = 'wrong password';
    }
</script>

It should close itself, if the login is true, and it does.
But in VSCode i got this error for { close } : Property 'close' does not exist on type 'unknown'. ts(2339)

@flekschas flekschas added the bug Something isn't working label Nov 8, 2022
@flekschas
Copy link
Owner

I supposed the types are not properly defined. Which version of the library are you using?

@KoljaL
Copy link
Author

KoljaL commented Nov 9, 2022

HI flekschas and thanks for the fast reply.

I'm using the current version: "svelte-simple-modal": "^1.4.1"

@flekschas
Copy link
Owner

Thanks for clarifying which version you're running. I realized that both the open() and close() functions do not have JSDoc type defs, which is why their type is unknown. Let me see if I can fix the issue with sveld (which auto-generates the types for this library)

@flekschas flekschas added improvement Improved feature and removed bug Something isn't working labels Nov 14, 2022
@flekschas
Copy link
Owner

Unfortunately, I am not yet sure how to properly type variables that are exposed via setContext but I've opened a ticket with Sveld: carbon-design-system/sveld#103.

In the meantime, I've improved the type definitions a bit so you should at least be able to manually declare the type as follows:

import type { Open, Close } from 'svelte-simple-modal';
const { open, close } = getContext('simple-modal') as { open: Open, close: Close };

I know this is only an interim solution but I hope it helps until I've figured out a better solution.

@HasanAboShally
Copy link

@flekschas, thanks for sharing the workaround.

On the line:
import type { Open, Close } from 'svelte-simple-modal';

I get the following error:

Module '"svelte-simple-modal"' has no exported member 'Open'. Did you mean to use 'import Open from "svelte-simple-modal"' instead?ts(2614).

Currently the following seems to work:

import type Close from 'svelte-simple-modal'; import type Open from 'svelte-simple-modal';

But now I receive an error on open(Popup, { message: "It's a modal!" });:
This expression is not callable. Type 'Modal' has no call signatures.ts(2349)

@flekschas
Copy link
Owner

Apologies, I had a typo. It should be

import { getContext } from 'svelte';
import type { Context } from 'svelte-simple-modal/types/Modal.svelte';
const { open, close } = getContext('simple-modal') as Context;

I'll see if I can get the Context exported from index.d.ts

@flekschas
Copy link
Owner

@HasanAboShally The typing should be fixed in v1.5.2 now. The following should not throw an error anymore:

<script lang="ts">
  import { getContext } from 'svelte';
  import { bind } from 'svelte-simple-modal';
  import type { Context } from 'svelte-simple-modal';

  import Popup from './popup.svelte';

  const { open } = getContext('svelte-simple-modal') as Context;

  function openModal() {
    open(bind(Popup, { message: 'It\'s a modal!' }));
  }
</script>

<button on:click={openModal}>Open a modal</button>

@LeoDog896
Copy link
Contributor

By the way, instead of using as Context, you can use getContext's generics:

<script lang="ts">
  import { getContext } from 'svelte';
  import { bind } from 'svelte-simple-modal';
  import type { Context } from 'svelte-simple-modal';

  import Popup from './popup.svelte';

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

  function openModal() {
    open(bind(Popup, { message: 'It\'s a modal!' }));
  }
</script>

<button on:click={openModal}>Open a modal</button>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement Improved feature
Projects
None yet
Development

No branches or pull requests

4 participants