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): support inferring inject #12964

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion types/common.d.ts
Expand Up @@ -5,7 +5,7 @@ export type UnionToIntersection<U> = (
) extends (k: infer I) => void
? I
: never

// Conditional returns can enforce identical types.
// See here: https://github.com/Microsoft/TypeScript/issues/27024#issuecomment-421529650
// prettier-ignore
Expand Down
28 changes: 21 additions & 7 deletions types/options.d.ts
Expand Up @@ -174,7 +174,9 @@ export interface ComponentOptions<
Props = DefaultProps,
RawBindings = {},
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
Inject extends InjectOptions = {},
InjectNames extends string = string,
> {
data?: Data
props?: PropsDef
Expand Down Expand Up @@ -225,7 +227,7 @@ export interface ComponentOptions<
filters?: { [key: string]: Function }

provide?: object | (() => object)
inject?: InjectOptions
inject?: Inject | InjectNames[]

model?: {
prop?: string
Expand Down Expand Up @@ -342,8 +344,20 @@ export interface DirectiveOptions {

export type InjectKey = string | symbol

export type InjectOptions =
| {
[key: string]: InjectKey | { from?: InjectKey; default?: any }
}
| string[]
export type InjectOptions = string[] | ObjectInjectOptions

type ObjectInjectOptions = Record<
InjectKey,
string | symbol | { from?: string | symbol; default?: unknown }
>

export type InjectToObject<T extends InjectOptions> =
T extends string[]
? {
[K in T[number]]?: unknown
}
: T extends ObjectInjectOptions
? {
[K in keyof T]?: unknown
}
: never
207 changes: 207 additions & 0 deletions types/test/v3/define-component-test.tsx
Expand Up @@ -1225,3 +1225,210 @@ describe('should report non-existent properties in instance', () => {
// @ts-expect-error
instance2.foo
})


describe('inject', () => {
test('with object inject', () => {
defineComponent({
props: {
a: String
},
inject: {
foo: 'foo',
bar: 'bar',
},
created() {
expectType<unknown>(this.foo)
expectType<unknown>(this.bar)
// @ts-expect-error
expectError(this.foobar = 1)
}
})
})

test('with array inject', () => {
defineComponent({
props: ['a', 'b'],
inject: ['foo', 'bar'],
created() {
expectType<unknown>(this.foo)
expectType<unknown>(this.bar)
// @ts-expect-error
expectError(this.foobar = 1)
}
})
})

test('with no props', () => {
defineComponent({
inject: {
foo: {
from: 'pfoo',
default: 'foo'
},
bar: {
from: 'pbar',
default: 'bar'
},
},
created() {
expectType<unknown>(this.foo)
expectType<unknown>(this.bar)
// @ts-expect-error
expectError(this.foobar = 1)
}
})
})

test('without inject', () => {
defineComponent({
props: ['a', 'b'],
created() {
// @ts-expect-error
expectError(this.foo = 1)
// @ts-expect-error
expectError(this.bar = 1)
}
})
})

test('define mixins w/ no props', () => {
const MixinA = defineComponent({
inject: {
foo: 'foo'
},
})
const MixinB = defineComponent({
inject: ['bar'],
})
// with no props
defineComponent({
mixins: [MixinA, MixinB],
created() {
expectType<unknown>(this.foo)
expectType<unknown>(this.bar)
}
})
// with object props
defineComponent({
mixins: [MixinA, MixinB],
props: {
baz: {
type: Number,
required: true
}
},
created() {
expectType<unknown>(this.foo)
expectType<unknown>(this.bar)
expectType<number>(this.baz)
}
})
// with array props
defineComponent({
mixins: [MixinA, MixinB],
props: ['baz'],
created() {
expectType<unknown>(this.foo)
expectType<unknown>(this.bar)
expectType<any>(this.baz)
}
})
})

test('define mixins w/ object props', () => {
const MixinA = defineComponent({
props: {
a: String
},
inject: {
foo: 'foo'
},
})
const MixinB = defineComponent({
props: {
b: String
},
inject: ['bar'],
})
// with no props
defineComponent({
mixins: [MixinA, MixinB],
created() {
expectType<unknown>(this.foo)
expectType<unknown>(this.bar)
}
})
// with object props
defineComponent({
mixins: [MixinA, MixinB],
props: {
baz: {
type: Number,
required: true
}
},
created() {
expectType<unknown>(this.foo)
expectType<unknown>(this.bar)
expectType<number>(this.baz)
}
})
// with array props
defineComponent({
mixins: [MixinA, MixinB],
props: ['baz'],
created() {
expectType<unknown>(this.foo)
expectType<unknown>(this.bar)
expectType<any>(this.baz)
}
})
})

test('define mixins w/ array props', () => {
const MixinA = defineComponent({
props: ['a'],
inject: {
foo: 'foo'
},
})
const MixinB = defineComponent({
props: ['b'],
inject: ['bar'],
})
// with no props
defineComponent({
mixins: [MixinA, MixinB],
created() {
expectType<unknown>(this.foo)
expectType<unknown>(this.bar)
}
})
// with object props
defineComponent({
mixins: [MixinA, MixinB],
props: {
baz: {
type: Number,
required: true
}
},
created() {
expectType<unknown>(this.foo)
expectType<unknown>(this.bar)
expectType<number>(this.baz)
}
})
// with array props
defineComponent({
mixins: [MixinA, MixinB],
props: ['baz'],
created() {
expectType<unknown>(this.foo)
expectType<unknown>(this.bar)
expectType<any>(this.baz)
}
})
})
})