Typing provide and inject
A value provided deep in a component tree is looked up by key. If the key is a plain string, nothing ties the value the provider passes to the value the consumer expects — the two can drift apart, and the mistake only shows at run time.
InjectionKey<T> fixes that. It is a symbol tagged with the value's type, so the
provider and the consumer share one typed key.
import type { InjectionKey } from 'vue';
export const messageKey: InjectionKey<string> = Symbol('message');
Export the key from a module both sides import. Then provide(messageKey, 'Hi')
and inject(messageKey) agree on the key and on the string it carries. A
consumer that injects a different key gets nothing, and a consumer that reads the
value as a number is a compile error. The type travels with the key, so both
sides are checked against the same value.
In App.vue, inject the message under the exported typed key.
Challenge
The message never arrives because the component injects the wrong key. Export an
App component from App.vue that declares and exports an
InjectionKey<string> and injects the message with it, rendering the result in a
<p>.
The Test provides a value under that key and reads the paragraph. The starter injects a plain string key, so it falls back to its default and the Test fails.