Skip to content
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

fix(types): remove number type of provide #6497

Closed
wants to merge 1 commit 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
8 changes: 6 additions & 2 deletions packages/dts-test/inject.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { expectType } from './utils'

// non-symbol keys
provide('foo', 123)
provide(123, 123)
provide(123, 123) // FIXME: should be a type error
provide([123], 123) // FIXME: should be a type error
provide({ deep: 'dark' }, 123) // FIXME: should be a type error

const key: InjectionKey<number> = Symbol()

Expand All @@ -12,6 +14,7 @@ provide(key, 1)
provide(key, 'foo')
// @ts-expect-error
provide(key, null)
provide<null | string>(key, null) // FIXME: should be a type error

expectType<number | undefined>(inject(key))
expectType<number>(inject(key, 1))
Expand All @@ -33,8 +36,9 @@ provide(injectionKeyRef, ref({}))

// naive-ui: explicit provide type parameter
provide<Cube>('cube', { size: 123 })
// @ts-expect-error
provide<Cube>(123, { size: 123 })
provide<Cube>(injectionKeyRef, { size: 123 })
provide<Cube>(injectionKeyRef, { size: 123 }) // FIXME: should be a type error

// @ts-expect-error
provide<Cube>('cube', { size: 'foo' })
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/apiInject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { warn } from './warning'

export interface InjectionKey<T> extends Symbol {}

export function provide<T, K = InjectionKey<T> | string | number>(
export function provide<T, K = InjectionKey<T> | string>(
key: K,
value: K extends InjectionKey<infer V> ? V : T
) {
Expand Down