Ensuring Exit Failure When VITE_POSTHOG_KEY is Empty in TypeScript

I have the below code, where this code try: () => import.meta.env.VITE_POSTHOG_KEY, always succeeds, but may be an emtpy string. How can I make it so that the Exit is of the failed variant if the VITE_POSTHOG_KEY is not an empty string?

import posthog from 'posthog-js';
import { browser } from '$app/environment';
import { Console, Effect, Exit } from 'effect';

const postHogKeyExit: Exit.Exit<string, Error> = Effect.runSyncExit(
    Effect.try({
        try: () => import.meta.env.VITE_POSTHOG_KEY,
        catch: (error) => new Error(`Missing posthog key. Is VITE_POSTHOG_KEY set? ${error}`)
    })
);

export const load = async () => {
    if (browser) {
        Exit.match(postHogKeyExit, {
            onSuccess: (postHogKey) =>
                posthog.init(postHogKey, {
                    api_host: 'https://us.i.posthog.com',
                    person_profiles: 'always'
                }),
            onFailure: (error) => Effect.runSync(Console.error(Effect.fail(error)))
        });
    }
    return;
};
Was this page helpful?