@@ -1191,14 +1191,13 @@ describe('async setup', () => {
11911191
11921192describe ( 'define attrs' , ( ) => {
11931193 test ( 'define attrs w/ object props' , ( ) => {
1194- type CompAttrs = {
1195- bar : number
1196- }
11971194 const MyComp = defineComponent ( {
11981195 props : {
11991196 foo : String
12001197 } ,
1201- attrs : Object as AttrsType < CompAttrs > ,
1198+ attrs : Object as AttrsType < {
1199+ bar ?: number
1200+ } > ,
12021201 created ( ) {
12031202 expectType < number | undefined > ( this . $attrs . bar )
12041203 }
@@ -1207,12 +1206,11 @@ describe('define attrs', () => {
12071206 } )
12081207
12091208 test ( 'define attrs w/ array props' , ( ) => {
1210- type CompAttrs = {
1211- bar : number
1212- }
12131209 const MyComp = defineComponent ( {
12141210 props : [ 'foo' ] ,
1215- attrs : Object as AttrsType < CompAttrs > ,
1211+ attrs : Object as AttrsType < {
1212+ bar ?: number
1213+ } > ,
12161214 created ( ) {
12171215 expectType < number | undefined > ( this . $attrs . bar )
12181216 }
@@ -1221,11 +1219,10 @@ describe('define attrs', () => {
12211219 } )
12221220
12231221 test ( 'define attrs w/ no props' , ( ) => {
1224- type CompAttrs = {
1225- bar : number
1226- }
12271222 const MyComp = defineComponent ( {
1228- attrs : Object as AttrsType < CompAttrs > ,
1223+ attrs : Object as AttrsType < {
1224+ bar ?: number
1225+ } > ,
12291226 created ( ) {
12301227 expectType < number | undefined > ( this . $attrs . bar )
12311228 }
@@ -1234,17 +1231,16 @@ describe('define attrs', () => {
12341231 } )
12351232
12361233 test ( 'define attrs w/ composition api' , ( ) => {
1237- type CompAttrs = {
1238- bar : number
1239- }
12401234 const MyComp = defineComponent ( {
12411235 props : {
12421236 foo : {
12431237 type : String ,
12441238 required : true
12451239 }
12461240 } ,
1247- attrs : Object as AttrsType < CompAttrs > ,
1241+ attrs : Object as AttrsType < {
1242+ bar ?: number
1243+ } > ,
12481244 setup ( props , { attrs } ) {
12491245 expectType < string > ( props . foo )
12501246 expectType < number | undefined > ( attrs . bar )
@@ -1254,9 +1250,6 @@ describe('define attrs', () => {
12541250 } )
12551251
12561252 test ( 'define attrs w/ functional component' , ( ) => {
1257- type CompAttrs = {
1258- bar : number
1259- }
12601253 const MyComp = defineComponent (
12611254 ( props : { foo : string } , ctx ) => {
12621255 expectType < number | undefined > ( ctx . attrs . bar )
@@ -1266,61 +1259,46 @@ describe('define attrs', () => {
12661259 )
12671260 } ,
12681261 {
1269- attrs : Object as AttrsType < CompAttrs >
1262+ attrs : Object as AttrsType < {
1263+ bar ?: number
1264+ } >
12701265 }
12711266 )
12721267 expectType < JSX . Element > ( < MyComp foo = { '1' } bar = { 1 } /> )
12731268 } )
12741269
12751270 test ( 'define attrs as low priority' , ( ) => {
1276- type CompAttrs = {
1277- foo : number
1278- }
12791271 const MyComp = defineComponent ( {
12801272 props : {
12811273 foo : String
12821274 } ,
1283- attrs : Object as AttrsType < CompAttrs > ,
1275+ attrs : Object as AttrsType < {
1276+ foo ?: number
1277+ } > ,
12841278 created ( ) {
12851279 // @ts -expect-error
12861280 this . $attrs . foo
1281+
1282+ expectType < string | undefined > ( this . foo ) ;
12871283 }
12881284 } )
12891285 expectType < JSX . Element > ( < MyComp foo = "1" /> )
12901286 } )
12911287
1292- test ( 'attrs is always optional w/ object props' , ( ) => {
1293- type CompAttrs = {
1294- bar : number
1295- }
1288+ test ( 'define required attrs' , ( ) => {
12961289 const MyComp = defineComponent ( {
1297- attrs : Object as AttrsType < CompAttrs > ,
1290+ attrs : Object as AttrsType < {
1291+ bar : number
1292+ } > ,
12981293 created ( ) {
12991294 expectType < number | undefined > ( this . $attrs . bar )
13001295 }
13011296 } )
1297+ expectType < JSX . Element > ( < MyComp bar = { 1 } /> )
1298+ // @ts -expect-error
13021299 expectType < JSX . Element > ( < MyComp /> )
13031300 } )
13041301
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-
13241302 test ( 'define attrs w/ no attrs' , ( ) => {
13251303 const MyComp = defineComponent ( {
13261304 props : {
@@ -1333,6 +1311,23 @@ describe('define attrs', () => {
13331311 // @ts -expect-error
13341312 expectType < JSX . Element > ( < MyComp foo = "1" bar = { 1 } /> )
13351313 } )
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+ } )
13361331} )
13371332
13381333// #5948
0 commit comments