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

Can't use own type in defineEmits in new Vue 3.3 structure #3223

Closed
szulcus opened this issue May 24, 2023 · 11 comments
Closed

Can't use own type in defineEmits in new Vue 3.3 structure #3223

szulcus opened this issue May 24, 2023 · 11 comments

Comments

@szulcus
Copy link

szulcus commented May 24, 2023

Work:

const emit = defineEmits<{
	close: [];
}>();

Don't work:

interface Emits {
	close: [];
}
const emit = defineEmits<Emits>();
Type 'Emits' does not satisfy the constraint '((...args: any[]) => any) | Record<string, any[]>'.
  Type 'Emits' is not assignable to type 'Record<string, any[]>'.ts(2344)

image
Previous structure works fine:
image

@jdk2pq
Copy link

jdk2pq commented May 26, 2023

Can confirm. Related: #3169 (comment)

@so1ve
Copy link
Member

so1ve commented May 28, 2023

try using type Emits = ...?

@jdk2pq
Copy link

jdk2pq commented May 30, 2023

@so1ve That did the trick! Thank you for the suggestion. This works great:

type Emits = {
    someEmit: [value: string];
}

const emits = defineEmits<Emits>();
emits('someEmit', value);

I also had to disable the @typescript-eslint/consistent-type-definitions ESLint rule.

@so1ve
Copy link
Member

so1ve commented May 30, 2023

Well that's a typescript limitation: microsoft/TypeScript#42825 but I think we can avoid it.

@so1ve
Copy link
Member

so1ve commented May 30, 2023

@jdk2pq Could you please confirm if this works:

type Simplify<T> = { [K in keyof T]: T[K] } & {}

interface Emits { ... }

defineEmits<Simplify<Emits>>()

?

@jdk2pq
Copy link

jdk2pq commented May 30, 2023

@jdk2pq Could you please confirm if this works:

type Simplify<T> = { [K in keyof T]: T[K] } & {}

interface Emits { ... }

defineEmits<Simplify<Emits>>()

?

Yep, that's passing vue-tsc for me as well 👍

@so1ve
Copy link
Member

so1ve commented May 30, 2023

Well this is not a fix (actually). The complex type cannot pass vue's compiler :( So, maybe a vue core's type problem? Can you try to import defineEmits from vue in a fresh ts file instead of a SFC and check whether this issue still exists? If so, that's a vue issue.

@jdk2pq
Copy link

jdk2pq commented May 30, 2023

@so1ve Sure thing. When I use the Simplify type, no issues:

Screenshot 2023-05-30 at 12 34 42 PM

But if I don't use that, I get the same type errors I was seeing before here #3169 (comment) :
Screenshot 2023-05-30 at 12 34 14 PM

No complaints when I use type Emits = { ... } either:
Screenshot 2023-05-30 at 12 36 35 PM

@so1ve
Copy link
Member

so1ve commented May 30, 2023

OK! Now we can confirm this is a vue issue.

@so1ve
Copy link
Member

so1ve commented May 30, 2023

Created an issue: vuejs/core#8457

@szulcus szulcus closed this as not planned Won't fix, can't repro, duplicate, stale May 30, 2023
@szulcus
Copy link
Author

szulcus commented May 30, 2023

Thanks @so1ve !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants