Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Use ContextKey<T> to type setContext, getContext and hasContext #8941

@olehmisar

Description

@olehmisar

Describe the problem

Currently getContext returns unknown if no explicit type is provided.
image

Describe the proposed solution

Taking inspiration from svelte-typed-context, getContext can accept an InjectionKey<T>ContextKey<T> and return T:
image

This will improve type safety and can be done in a backwards compatible way.

The implementation would be just to add a new signature to getContext, setContext and hasContext

export interface ContextKey<T = unknown> { }
export function setContext<T>(key: ContextKey<T>, context: T): void
// getContext ...
// hasContext ...
// check for full implementation here:
// https://github.com/KamenKolev/svelte-typed-context/blob/master/index.ts
// (published npm package does not include `hasContext`, so install only from github)

Alternatives considered

Importance

nice to have

Search terms

typed context inject provide

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions