A keyless and typesafe approach to the Svelte context API
npm i svelte-create-context
// context.ts
import { createContext } from 'svelte-create-context';
export const {
setup: setupCurrentUser,
get: getCurrentUser
} = createContext<User>();
// parent component
<script lang="ts">
import { setupCurrentUser } from './context';
setupCurrentUser(new User());
</script>
// child component
<script lang="ts">
import { getCurrentUser } from './context';
const currentUser = getCurrentUser();
</script>
interface Context<T> {
setup: (value: T) => void;
get: () => T;
}
function createContext<T>(): Context<T>