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

Binding ClerkLoaded results in type error #68

Open
GodTamIt opened this issue Jul 19, 2024 · 3 comments
Open

Binding ClerkLoaded results in type error #68

GodTamIt opened this issue Jul 19, 2024 · 3 comments

Comments

@GodTamIt
Copy link

Simple example:

<script lang="ts">
    import ClerkLoaded from 'clerk-sveltekit/client/ClerkLoaded.svelte';
    import Clerk from '@clerk/clerk-js';

    let clerk: Clerk | null;

    $: {
        if (clerk?.user != null) {
            // Do something interesting
        }
    }
</script>

<ClerkLoaded bind:clerk />
//                ^^^^^
//  Type 'Clerk | null' is not assignable to type 'never'.

I'm doing this because I want to access clerk from Typescript/Javascript.

@wobsoriano
Copy link

wobsoriano commented Jul 29, 2024

When this PR gets merged, you should be able to subscribe to the Clerk store:

<script lang="ts">
    import { clerk, user } from 'clerk-sveltekit/client/store'

    $: console.log('user id', $clerk?.user.id)
    $: console.log('user id', $user?.id)
</script>

@GodTamIt
Copy link
Author

GodTamIt commented Aug 2, 2024

@wobsoriano until then, do you know what the idiomatic thing to do today is, if you want to access the value in JS?

also, thanks for the heads up!

@wobsoriano
Copy link

You can do something like this:

let user
onMount(() => {
    window.Clerk.addListener((payload) => {
	user = payload.user
    })
})

But make sure Clerk is added to the window object before doing that. You have two options:

  1. Wrap the component with <ClerkLoaded>
  2. Hack and wait for window.Clerk. to be available (via intervals)

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

No branches or pull requests

2 participants