@288-toolkit/typed-context

3.0.0 • Public • Published

Typed-context

Wraps typed versions of Svelte's getContext and setContext functions with the provided type argument and context key.

Usage

my-context.ts

import { createTypedContext } from '@288-toolkit/typed-context';

type MyContext = {
	hello: string;
};

export const { getContext, setContext } = createTypedContext<MyContext>('my-context');

my-parent-component.ts

<script lang="ts">
	import { setContext } from './my-context.ts';

	setContext({
		hello: 'world',
		other: 'prop' // Type error
	});
</script>

my-child-component.ts

<script lang="ts">
	import { getContext } from './my-context.ts';

	const ctx = getContext();

	console.log(ctx.hello); // 'world'
	console.log(ctx.other); // Type error
</script>

Readme

Keywords

none

Package Sidebar

Install

npm i @288-toolkit/typed-context

Weekly Downloads

92

Version

3.0.0

License

MIT

Unpacked Size

2.4 kB

Total Files

4

Last publish

Collaborators

  • nitriques
  • f-elix