You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[TS Playground](https://www.typescriptlang.org/play?jsx=1#code/JYWwDg9gTgLgBAbzgEwKYDNgDtUGELgQ5bwC+c6UBcA5AG4CuqAtDAM7MMzAA2bNAWABQwgMZE28fODgBeFBmx4CkYjAAUCYXDhgqYNgC5E2nRQgRjAZRhRsAcwA0p0s6E6oqLGijqAlIikwq6IcACGMLZGgeFsoQBGYVDGWAwg8ahQcOSkfsJiEvDiMvIAPNJg5hCyCDQAjDTkiVA1deQA9AB8wkA) with Options Api
[TS Playground](https://www.typescriptlang.org/play?jsx=1#code/JYWwDg9gTgLgBAbzgEwKYDNgDtUGELgQ5bwC+c6UBcA5AG4CuqAtDAM7MMzAA2bNAWABQwgMZE28fODgBeFBmx4CkYjAAUCYXDhgqYNgC5E2nRQgRjAZRhRsAcwA0p0s6E62qGAzDq9EA0dEOABDGFs2OFIAShN3M3EsNggeVAA6Hgh7P302NPQLWIB6IrhJOyx7Ux1E5NSMrPUwiLSAIxCoYtKsBhBW1CgXYVdg5qgjRHIQyKR2qGMevoGoqOjhMQl4cRl5AB5pMHMIWQQaAEYacjmTs-IigD5hIA) with Composition Api
31
+
```tsx
32
+
const Comp =defineComponent({
33
+
props: {
34
+
foo: String
35
+
},
36
+
setup(props, { attrs }) {
37
+
// number
38
+
console.log(attrs.bar)
39
+
}
40
+
}, { attrs: {} as { bar:number } })
31
41
32
-
```vue
33
-
<script setup lang="ts">
34
-
const attrs = useAttrs<{bar: number}>()
35
-
</script>
42
+
const comp = <Compfoo={'str'}bar={1} />
36
43
```
37
44
38
-
<details>
39
-
<summary>Compiled Output</summary>
40
45
41
-
```js
42
-
exportdefault/*#__PURE__*/_defineComponent({
43
-
setup(__props, { expose }) {
44
-
expose();
46
+
[TS Playground](https://www.typescriptlang.org/play?jsx=1#code/JYWwDg9gTgLgBAbzgEwKYDNgDtUGELgQ5bwC+c6UBcA5AG4CuqAtDAM7MMzAA2bNAWABQwgMZE28fODgBeFBmx4CkYjAAUCYXDhgqYNgC5E2nRQgRjAZRhRsAcwA0p0s6E62qGAzDq9EA0dEOABDGFs2OFIAShN3M3EsNggeVAA6Hgh7P302NPQLWIB6IrhJOyx7Ux1E5NSMrPUwiLSAIxCoYtKsBhBW1CgXYVdg5qgjRHIQyKR2qGMevoGoqOjhMQl4cRl5AB5pMHMIWQQaAEYacjmTs-IigD5hIA) with functional Components
This proposal is mainly to infer `attrs` using `defineComponent`.
73
100
74
-
When using typescript in Vue3, the fallthrough attributes is unable to be used. It's not appropriate obviously that only one can be chosen from `typescript` and `the fallthrough attributes`. In most cases, we choose `typescript` and set attributes to `props` options instead of using the fallthrough attributes.
101
+
When using typescript in Vue3, the fallthrough attributes is unable to be used. It's not appropriate obviously that only one can be chosen from `typescript` and `the fallthrough attributes`. In most cases, we choose `typescript` and set attributes to `props`option instead of using the fallthrough attributes.
75
102
103
+
Main scenes:
104
+
105
+
- Enhancing native html component, such as `button`, `input`...
106
+
- Wrapping a component from UI library, such as `el-button` from [element-plus](https://github.com/element-plus/element-plus).
76
107
77
108
# Detailed design
78
109
@@ -83,9 +114,10 @@ Due to typescript limitation from [microsoft/TypeScript#10571](https://github.co
83
114
const Comp =defineComponent<Props, Attrs>({})
84
115
```
85
116
117
+
86
118
There still has two ways to be chosen.
87
119
88
-
1. Defining the first param that already existing, just like [vuejs/rfcs#192](https://github.com/vuejs/rfcs/pull/192) did.
120
+
### 1. Defining the first param that already existing, just like [vuejs/rfcs#192](https://github.com/vuejs/rfcs/pull/192) did.
At last i chosen the second way that pass `attrs` type to the second params of `defineComponent`, because I think the code of the component should not be involved just for type definition.
150
+
At last I chosen the second way that passing`attrs` type to the second params of `defineComponent`, because I think the code of the component should not be involved just for type definition.
119
151
120
152
121
153
The following below is the design details.
122
154
-`attrs` is inferred to `{ class: unknown; style: unknown }` when the value of the second param is `undefined`
123
155
-`attrs` is lower priority than `props`.
124
-
- [see for more detail cases](https://github.com/vuejs/core/pull/7444/files)
156
+
-[see for more detail cases](https://github.com/vuejs/core/pull/7444/files#diff-241bba82b0b4ebadd7a9c19ed82eed97283874b6d15ed32d62c05184e29ecb91R1195-R1306)
125
157
126
158
## `useAttrs<T>`
127
159
In the `setup-script`, the generic type of `useAttrs` will compile to the second param of `defineComponent`.
0 commit comments