Skip to content

Commit 94b2a26

Browse files
committed
tsx event type,2.1.1
1 parent fc40725 commit 94b2a26

File tree

6 files changed

+17
-7
lines changed

6 files changed

+17
-7
lines changed

docs/en/tsx/attribute-types/attribute-types.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ To make enable TSX attribute types:
1010

1111
4. Make component extend from `TSX<Props,Events>()(BaseComponent)`.
1212

13-
> The keys of `Events` will be capitalized and prefixed by `on`. e.g. `myEvent` => `onMyEvent`.
13+
> The keys of `Events` will be capitalized and prefixed by `on`. e.g. `myEvent` => `onMyEvent`.
14+
15+
> If a `Events` value has a non-function type, it's type will be transformed to a function that accepts only one parameter typed by the value's type and returns `any` type. e.g. `{myEvent:string}` => `{myEvent:(param:string)=>any}`.
1416
1517
> There are two `()`s after `TSX<Props,Events>`.
1618

docs/en/tsx/attribute-types/code-usage.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ interface Props {
66

77
interface Events {
88
myEvent: Function
9+
myEvent2: string
910
}
1011

1112
@Component
@@ -18,9 +19,13 @@ export default class MyComponent extends TSX<Props, Events>()(Vue) {
1819
triggerMyEvent() {
1920
return 'event value'
2021
}
22+
@Emit('myEvent2')
23+
triggerMyEvent2() {
24+
return 'event2 value'
25+
}
2126
}
2227

2328
//TypeScript will check component attributes in TSX.
2429
function render() {
25-
return <MyComponent propString='foobar' onMyEvent={(v: string) => { }}></MyComponent>
30+
return <MyComponent propString='foobar' onMyEvent={(v: string) => { }} onMyEvent2={(v:string)=>{}}></MyComponent>
2631
}

docs/zh-cn/tsx/attribute-types/attribute-types.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
> `Events`中的键会变为首字母大写并被加前缀`on`,例如`myEvent` => `onMyEvent`
1414
15+
> 如果一个`Events`的值是非函数类型,它的类型会被转换为一个函数,这个函数仅仅接受一个前面定义值的类型的参数并且返回`any`类型。例如`{myEvent:string}` => `{myEvent:(param:string)=>any}`
16+
1517
> `TSX<Props,Events>`后有两个`()`
1618
1719
[](./code-usage.tsx ':include :type=code tsx')

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-facing-decorator",
3-
"version": "2.1.1",
3+
"version": "2.1.2",
44
"description": "Vue typescript class and decorator based component.",
55
"main": "dist/index.js",
66
"keywords": [

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export interface BaseTypeIdentify {
1616
export function TSX<Properties extends {} = {}, Events extends {} = {}>() {
1717

1818

19-
type Bundle = Properties & { [index in keyof Events as `on${Capitalize<index & string>}`]: Events[index] }
19+
type Bundle = Properties & { [index in keyof Events as `on${Capitalize<index & string>}`]: Events[index] extends Function ? Events[index] : { (param: Events[index]): any } }
2020
return function <C extends { new(): ComponentPublicInstance & BaseTypeIdentify }>(cons: C) {
2121
return cons as unknown as {
2222
new(): Omit<ComponentPublicInstance<(InstanceType<C>['$props']) & Bundle>, keyof Bundle> & InstanceType<C>//& ComponentPublicInstance & BaseTypeIdentify

test/tsx/attributeTypes.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import { Component, TSX, Prop, Base } from '../../dist'
22
interface Props {
33
p: string
44
}
5-
interface Events{
6-
e:Function
5+
interface Events {
6+
e: Function
7+
e2: string
78
}
89
@Component
9-
class TsxAttributeTypes extends TSX<Props,Events>()(Base) implements Props {
10+
class TsxAttributeTypes extends TSX<Props, Events>()(Base) implements Props {
1011
@Prop({
1112
required: true
1213
})

0 commit comments

Comments
 (0)