admin管理员组文章数量:1405393
There is defineEmits
in Vue 2.7 (composition API)
We can use it that way:
const emit = defineEmit<{
(e: 'event-1', payload: number): void
(e: 'event-2', payload: string): void
}>()
and than use:
emit('event-1', 2)
emit('event-2', '2')
typescript understand types correctly
and when adding listeners in parent component
@event-1="(p: string) => console.log(p) //error p type should be number
vscode understands somehow parameters type
I wonder how it works, cause generic utility type "Parameters" always returns last overloading signature (event-2) as it's written in docs
So my question is: how to typefy function with function interface (not mapped types) that it understands parameter types correctly... something like that:
interface mySuperInterface{
(e: 'event-1', payload: number): void
(e: 'event-2', payload: string): void
}
function mySuperFunction<T extends (...args: any) => void>() {
return function() {
console.log(params)
}
const d = mySuperFunction<mySuperInterface>()
d('event-1', 1) // correct
d('event-2', 1) // not correct
tried to use Parameters utility type, but it uses infer, that takes last overloaded signature (event-2 in my case) I know how to do that with mapped types, but how it's done in defineEmits?
There is defineEmits
in Vue 2.7 (composition API)
We can use it that way:
const emit = defineEmit<{
(e: 'event-1', payload: number): void
(e: 'event-2', payload: string): void
}>()
and than use:
emit('event-1', 2)
emit('event-2', '2')
typescript understand types correctly
and when adding listeners in parent component
@event-1="(p: string) => console.log(p) //error p type should be number
vscode understands somehow parameters type
I wonder how it works, cause generic utility type "Parameters" always returns last overloading signature (event-2) as it's written in docs
So my question is: how to typefy function with function interface (not mapped types) that it understands parameter types correctly... something like that:
interface mySuperInterface{
(e: 'event-1', payload: number): void
(e: 'event-2', payload: string): void
}
function mySuperFunction<T extends (...args: any) => void>() {
return function() {
console.log(params)
}
const d = mySuperFunction<mySuperInterface>()
d('event-1', 1) // correct
d('event-2', 1) // not correct
tried to use Parameters utility type, but it uses infer, that takes last overloaded signature (event-2 in my case) I know how to do that with mapped types, but how it's done in defineEmits?
Share Improve this question asked Mar 9 at 10:13 SolidSolid 1011 silver badge1 bronze badge1 Answer
Reset to default 1I wouldn't mind just to assert:
Playground
function mySuperFunction<T extends (...args: any) => void>() {
return function(...params: any) {
console.log(params)
} as T;
}
Intellisense is ok:
本文标签: typescriptHow to typify like defineExpose in vuejsStack Overflow
版权声明:本文标题:typescript - How to typify like defineExpose in vuejs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744873134a2629751.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论