@@ -1191,14 +1191,13 @@ describe('async setup', () => {
1191
1191
1192
1192
describe ( 'define attrs' , ( ) => {
1193
1193
test ( 'define attrs w/ object props' , ( ) => {
1194
- type CompAttrs = {
1195
- bar : number
1196
- }
1197
1194
const MyComp = defineComponent ( {
1198
1195
props : {
1199
1196
foo : String
1200
1197
} ,
1201
- attrs : Object as AttrsType < CompAttrs > ,
1198
+ attrs : Object as AttrsType < {
1199
+ bar ?: number
1200
+ } > ,
1202
1201
created ( ) {
1203
1202
expectType < number | undefined > ( this . $attrs . bar )
1204
1203
}
@@ -1207,12 +1206,11 @@ describe('define attrs', () => {
1207
1206
} )
1208
1207
1209
1208
test ( 'define attrs w/ array props' , ( ) => {
1210
- type CompAttrs = {
1211
- bar : number
1212
- }
1213
1209
const MyComp = defineComponent ( {
1214
1210
props : [ 'foo' ] ,
1215
- attrs : Object as AttrsType < CompAttrs > ,
1211
+ attrs : Object as AttrsType < {
1212
+ bar ?: number
1213
+ } > ,
1216
1214
created ( ) {
1217
1215
expectType < number | undefined > ( this . $attrs . bar )
1218
1216
}
@@ -1221,11 +1219,10 @@ describe('define attrs', () => {
1221
1219
} )
1222
1220
1223
1221
test ( 'define attrs w/ no props' , ( ) => {
1224
- type CompAttrs = {
1225
- bar : number
1226
- }
1227
1222
const MyComp = defineComponent ( {
1228
- attrs : Object as AttrsType < CompAttrs > ,
1223
+ attrs : Object as AttrsType < {
1224
+ bar ?: number
1225
+ } > ,
1229
1226
created ( ) {
1230
1227
expectType < number | undefined > ( this . $attrs . bar )
1231
1228
}
@@ -1234,17 +1231,16 @@ describe('define attrs', () => {
1234
1231
} )
1235
1232
1236
1233
test ( 'define attrs w/ composition api' , ( ) => {
1237
- type CompAttrs = {
1238
- bar : number
1239
- }
1240
1234
const MyComp = defineComponent ( {
1241
1235
props : {
1242
1236
foo : {
1243
1237
type : String ,
1244
1238
required : true
1245
1239
}
1246
1240
} ,
1247
- attrs : Object as AttrsType < CompAttrs > ,
1241
+ attrs : Object as AttrsType < {
1242
+ bar ?: number
1243
+ } > ,
1248
1244
setup ( props , { attrs } ) {
1249
1245
expectType < string > ( props . foo )
1250
1246
expectType < number | undefined > ( attrs . bar )
@@ -1254,9 +1250,6 @@ describe('define attrs', () => {
1254
1250
} )
1255
1251
1256
1252
test ( 'define attrs w/ functional component' , ( ) => {
1257
- type CompAttrs = {
1258
- bar : number
1259
- }
1260
1253
const MyComp = defineComponent (
1261
1254
( props : { foo : string } , ctx ) => {
1262
1255
expectType < number | undefined > ( ctx . attrs . bar )
@@ -1266,61 +1259,46 @@ describe('define attrs', () => {
1266
1259
)
1267
1260
} ,
1268
1261
{
1269
- attrs : Object as AttrsType < CompAttrs >
1262
+ attrs : Object as AttrsType < {
1263
+ bar ?: number
1264
+ } >
1270
1265
}
1271
1266
)
1272
1267
expectType < JSX . Element > ( < MyComp foo = { '1' } bar = { 1 } /> )
1273
1268
} )
1274
1269
1275
1270
test ( 'define attrs as low priority' , ( ) => {
1276
- type CompAttrs = {
1277
- foo : number
1278
- }
1279
1271
const MyComp = defineComponent ( {
1280
1272
props : {
1281
1273
foo : String
1282
1274
} ,
1283
- attrs : Object as AttrsType < CompAttrs > ,
1275
+ attrs : Object as AttrsType < {
1276
+ foo ?: number
1277
+ } > ,
1284
1278
created ( ) {
1285
1279
// @ts -expect-error
1286
1280
this . $attrs . foo
1281
+
1282
+ expectType < string | undefined > ( this . foo ) ;
1287
1283
}
1288
1284
} )
1289
1285
expectType < JSX . Element > ( < MyComp foo = "1" /> )
1290
1286
} )
1291
1287
1292
- test ( 'attrs is always optional w/ object props' , ( ) => {
1293
- type CompAttrs = {
1294
- bar : number
1295
- }
1288
+ test ( 'define required attrs' , ( ) => {
1296
1289
const MyComp = defineComponent ( {
1297
- attrs : Object as AttrsType < CompAttrs > ,
1290
+ attrs : Object as AttrsType < {
1291
+ bar : number
1292
+ } > ,
1298
1293
created ( ) {
1299
1294
expectType < number | undefined > ( this . $attrs . bar )
1300
1295
}
1301
1296
} )
1297
+ expectType < JSX . Element > ( < MyComp bar = { 1 } /> )
1298
+ // @ts -expect-error
1302
1299
expectType < JSX . Element > ( < MyComp /> )
1303
1300
} )
1304
1301
1305
- test ( 'attrs is always optional w/ functional component' , ( ) => {
1306
- type CompAttrs = {
1307
- bar : number
1308
- }
1309
- const MyComp = defineComponent (
1310
- ( props : { foo : string } , ctx ) => {
1311
- expectType < number | undefined > ( ctx . attrs . bar )
1312
- return ( ) => (
1313
- // return a render function (both JSX and h() works)
1314
- < div > { props . foo } </ div >
1315
- )
1316
- } ,
1317
- {
1318
- attrs : Object as AttrsType < CompAttrs >
1319
- }
1320
- )
1321
- expectType < JSX . Element > ( < MyComp foo = { '1' } /> )
1322
- } )
1323
-
1324
1302
test ( 'define attrs w/ no attrs' , ( ) => {
1325
1303
const MyComp = defineComponent ( {
1326
1304
props : {
@@ -1333,6 +1311,23 @@ describe('define attrs', () => {
1333
1311
// @ts -expect-error
1334
1312
expectType < JSX . Element > ( < MyComp foo = "1" bar = { 1 } /> )
1335
1313
} )
1314
+
1315
+ test ( 'default attrs like class, style' , ( ) => {
1316
+ const MyComp = defineComponent ( {
1317
+ props : {
1318
+ foo : String
1319
+ } ,
1320
+ attrs : Object as AttrsType < {
1321
+ bar ?: number
1322
+ } > ,
1323
+ created ( ) {
1324
+ expectType < number | undefined > ( this . $attrs . bar )
1325
+ expectType < unknown > ( this . $attrs . class )
1326
+ expectType < unknown > ( this . $attrs . style )
1327
+ }
1328
+ } )
1329
+ expectType < JSX . Element > ( < MyComp class = { 'str' } style = { 'str' } foo = "1" bar = { 1 } /> )
1330
+ } )
1336
1331
} )
1337
1332
1338
1333
// #5948
0 commit comments