Skip to content

types(runtime-core): Allow InjectionKey to be used as a valid PropertyKey #5089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/dts-test/inject.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { expectType } from './utils'
provide('foo', 123)
provide(123, 123)

const key: InjectionKey<number> = Symbol()
const key = Symbol() as InjectionKey<number>

provide(key, 1)
// @ts-expect-error
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/__tests__/apiInject.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('api: provide/inject', () => {

it('symbol keys', () => {
// also verifies InjectionKey type sync
const key: InjectionKey<number> = Symbol()
const key = Symbol() as InjectionKey<number>

const Provider = {
setup() {
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/apiCreateApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export interface App<HostElement = any> {
): ComponentPublicInstance
unmount(): void
onUnmount(cb: () => void): void
provide<T, K = InjectionKey<T> | string | number>(
provide<T, K = InjectionKey<T> | string | symbol | number>(
key: K,
value: K extends InjectionKey<infer V> ? V : T,
): this
Expand Down
13 changes: 7 additions & 6 deletions packages/runtime-core/src/apiInject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { currentRenderingInstance } from './componentRenderContext'
import { currentApp } from './apiCreateApp'
import { warn } from './warning'

export interface InjectionKey<T> extends Symbol {}
declare const InjectionKeySymbol: unique symbol
export type InjectionKey<T> = symbol & { [InjectionKeySymbol]: T }

export function provide<T, K = InjectionKey<T> | string | number>(
export function provide<T, K = InjectionKey<T> | string | number | symbol>(
key: K,
value: K extends InjectionKey<infer V> ? V : T,
) {
Expand All @@ -31,19 +32,19 @@ export function provide<T, K = InjectionKey<T> | string | number>(
}
}

export function inject<T>(key: InjectionKey<T> | string): T | undefined
export function inject<T>(key: InjectionKey<T> | string | symbol): T | undefined
export function inject<T>(
key: InjectionKey<T> | string,
key: InjectionKey<T> | string | symbol,
defaultValue: T,
treatDefaultAsFactory?: false,
): T
export function inject<T>(
key: InjectionKey<T> | string,
key: InjectionKey<T> | string | symbol,
defaultValue: T | (() => T),
treatDefaultAsFactory: true,
): T
export function inject(
key: InjectionKey<any> | string,
key: InjectionKey<any> | string | symbol,
defaultValue?: unknown,
treatDefaultAsFactory = false,
) {
Expand Down