-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: resolve conflicts with vue2 interface (#869)
- Loading branch information
1 parent
73524d2
commit e84b488
Showing
5 changed files
with
85 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { | ||
defineComponent, | ||
describe, | ||
expectError, | ||
expectType, | ||
Ref, | ||
ref, | ||
} from './index' | ||
import Vue from 'vue' | ||
|
||
describe('emits', () => { | ||
const testComponent = defineComponent({ | ||
emits: { | ||
click: (n: number) => typeof n === 'number', | ||
input: (b: string) => b.length > 1, | ||
}, | ||
created() { | ||
this.$emit('click', 1) | ||
this.$emit('click', 1).$emit('click', 1) | ||
this.$emit('input', 'foo') | ||
this.$emit('input', 'foo').$emit('click', 1) | ||
expectType<Record<string, string>>(this.$attrs) | ||
// @ts-expect-error | ||
expectError(this.$emit('input', 1).$emit('nope')) | ||
}, | ||
}) | ||
|
||
// interface of vue2's $emit has no generics, notice that untyped types will be "event: string, ...args: any[]) => this" when using vue-class-component. | ||
// but we can get correct type when we use correct params | ||
// maybe we need vue 2.7 to fully support emit type | ||
type VueClass<V> = { new (...args: any[]): V & Vue } & typeof Vue | ||
|
||
function useComponentRef<T extends VueClass<Vue>>() { | ||
return ref<InstanceType<T>>(undefined!) as Ref<InstanceType<T>> | ||
} | ||
|
||
const foo = useComponentRef<typeof testComponent>() | ||
|
||
foo.value.$emit('click', 1) | ||
foo.value.$emit('input', 'foo') | ||
foo.value.$emit('click', 1).$emit('click', 1) | ||
// @ts-expect-error | ||
expectError(foo.value.$emit('blah').$emit('click', 1)) | ||
// @ts-expect-error | ||
expectError(foo.value.$emit('click').$emit('click', 1)) | ||
// @ts-expect-error | ||
expectError(foo.value.$emit('blah').$emit('click', 1)) | ||
// @ts-expect-error | ||
expectError(foo.value.$emit('blah')) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters