2
2
type Component ,
3
3
type ComponentOptions ,
4
4
type ComponentPublicInstance ,
5
+ type DefineSetupFnComponent ,
5
6
type PropType ,
6
7
type SetupContext ,
7
8
type Slots ,
@@ -1411,6 +1412,14 @@ describe('function syntax w/ runtime props', () => {
1411
1412
} ,
1412
1413
)
1413
1414
1415
+ expectType < JSX . Element > ( < Comp1 msg = "1" /> )
1416
+ // @ts -expect-error msg type is incorrect
1417
+ expectType < JSX . Element > ( < Comp1 msg = { 1 } /> )
1418
+ // @ts -expect-error msg is missing
1419
+ expectType < JSX . Element > ( < Comp1 /> )
1420
+ // @ts -expect-error bar doesn't exist
1421
+ expectType < JSX . Element > ( < Comp1 msg = "1" bar = "2" /> )
1422
+
1414
1423
// @ts -expect-error bar isn't specified in props definition
1415
1424
defineComponent (
1416
1425
( _props : { msg : string } ) => {
@@ -1430,13 +1439,15 @@ describe('function syntax w/ runtime props', () => {
1430
1439
} ,
1431
1440
)
1432
1441
1433
- expectType < JSX . Element > ( < Comp1 msg = "1" /> )
1434
- // @ts -expect-error msg type is incorrect
1435
- expectType < JSX . Element > ( < Comp1 msg = { 1 } /> )
1436
- // @ts -expect-error msg is missing
1437
- expectType < JSX . Element > ( < Comp1 /> )
1438
- // @ts -expect-error bar doesn't exist
1439
- expectType < JSX . Element > ( < Comp1 msg = "1" bar = "2" /> )
1442
+ // @ts -expect-error string prop names don't match
1443
+ defineComponent (
1444
+ ( _props : { msg : string } ) => {
1445
+ return ( ) => { }
1446
+ } ,
1447
+ {
1448
+ props : [ 'bar' ] ,
1449
+ } ,
1450
+ )
1440
1451
1441
1452
const Comp2 = defineComponent (
1442
1453
< T extends string > ( _props : { msg : T } ) => {
@@ -1447,8 +1458,16 @@ describe('function syntax w/ runtime props', () => {
1447
1458
} ,
1448
1459
)
1449
1460
1450
- // @ts -expect-error bar isn't specified in props definition
1451
- defineComponent (
1461
+ expectType < JSX . Element > ( < Comp2 msg = "1" /> )
1462
+ expectType < JSX . Element > ( < Comp2 < string > msg = "1" /> )
1463
+ // @ts -expect-error msg type is incorrect
1464
+ expectType < JSX . Element > ( < Comp2 msg = { 1 } /> )
1465
+ // @ts -expect-error msg is missing
1466
+ expectType < JSX . Element > ( < Comp2 /> )
1467
+ // @ts -expect-error bar doesn't exist
1468
+ expectType < JSX . Element > ( < Comp2 msg = "1" bar = "2" /> )
1469
+
1470
+ const Comp3 = defineComponent (
1452
1471
< T extends string > ( _props : { msg : T } ) => {
1453
1472
return ( ) => { }
1454
1473
} ,
@@ -1457,6 +1476,12 @@ describe('function syntax w/ runtime props', () => {
1457
1476
} ,
1458
1477
)
1459
1478
1479
+ // This is not the preferred behavior because it's better to see a typescript error,
1480
+ // but this is a compromise to resolve a relatively worse problem -
1481
+ // not inferring props types from runtime props when the types are not explicitly set.
1482
+ // See #13119#discussion_r2137831991
1483
+ expectType < DefineSetupFnComponent < { msg : any ; bar : any } > > ( Comp3 )
1484
+
1460
1485
defineComponent (
1461
1486
< T extends string > ( _props : { msg : T ; bar : T } ) => {
1462
1487
return ( ) => { }
@@ -1466,17 +1491,9 @@ describe('function syntax w/ runtime props', () => {
1466
1491
} ,
1467
1492
)
1468
1493
1469
- expectType < JSX . Element > ( < Comp2 msg = "1" /> )
1470
- expectType < JSX . Element > ( < Comp2 < string > msg = "1" /> )
1471
- // @ts -expect-error msg type is incorrect
1472
- expectType < JSX . Element > ( < Comp2 msg = { 1 } /> )
1473
- // @ts -expect-error msg is missing
1474
- expectType < JSX . Element > ( < Comp2 /> )
1475
- // @ts -expect-error bar doesn't exist
1476
- expectType < JSX . Element > ( < Comp2 msg = "1" bar = "2" /> )
1477
-
1478
1494
// Note: generics aren't supported with object runtime props
1479
- const Comp3 = defineComponent (
1495
+ // so the props will infer the runtime props' types
1496
+ const Comp4 = defineComponent (
1480
1497
< T extends string > ( _props : { msg : T } ) => {
1481
1498
return ( ) => { }
1482
1499
} ,
@@ -1487,6 +1504,17 @@ describe('function syntax w/ runtime props', () => {
1487
1504
} ,
1488
1505
)
1489
1506
1507
+ expectType < DefineSetupFnComponent < { msg : string } > > ( Comp4 )
1508
+ expectType < JSX . Element > ( < Comp4 msg = "1" /> )
1509
+ // @ts -expect-error generics aren't supported with object runtime props
1510
+ expectType < JSX . Element > ( < Comp4 < string > msg = "1" /> )
1511
+ // @ts -expect-error msg type is incorrect
1512
+ expectType < JSX . Element > ( < Comp4 msg = { 1 } /> )
1513
+ // @ts -expect-error msg is missing
1514
+ expectType < JSX . Element > ( < Comp4 /> )
1515
+ // @ts -expect-error bar doesn't exist
1516
+ expectType < JSX . Element > ( < Comp4 msg = "1" bar = "2" /> )
1517
+
1490
1518
defineComponent (
1491
1519
// @ts -expect-error bar isn't specified in props definition
1492
1520
< T extends string > ( _props : { msg : T } ) => {
@@ -1500,46 +1528,35 @@ describe('function syntax w/ runtime props', () => {
1500
1528
)
1501
1529
1502
1530
defineComponent (
1503
- // @ts -expect-error generics aren't supported with object runtime props
1504
- < T extends string > ( _props : { msg : T ; bar : T } ) => {
1531
+ ( _props : { msg : string } ) => {
1505
1532
return ( ) => { }
1506
1533
} ,
1507
1534
{
1508
1535
props : {
1509
- msg : String ,
1536
+ // @ts -expect-error prop type mismatch
1537
+ msg : Number ,
1510
1538
} ,
1511
1539
} ,
1512
1540
)
1513
1541
1514
- expectType < JSX . Element > ( < Comp3 msg = "1" /> )
1515
- // @ts -expect-error generics aren't supported with object runtime props
1516
- expectType < JSX . Element > ( < Comp3 < string > msg = "1" /> )
1517
- // @ts -expect-error msg type is incorrect
1518
- expectType < JSX . Element > ( < Comp3 msg = { 1 } /> )
1519
- // @ts -expect-error msg is missing
1520
- expectType < JSX . Element > ( < Comp3 /> )
1521
- // @ts -expect-error bar doesn't exist
1522
- expectType < JSX . Element > ( < Comp3 msg = "1" bar = "2" /> )
1523
-
1524
- // @ts -expect-error string prop names don't match
1525
- defineComponent (
1526
- ( _props : { msg : string } ) => {
1542
+ const Comp5 = defineComponent (
1543
+ _props => {
1527
1544
return ( ) => { }
1528
1545
} ,
1529
1546
{
1530
- props : [ 'bar ' ] ,
1547
+ props : [ 'foo ' ] ,
1531
1548
} ,
1532
1549
)
1533
1550
1551
+ expectType < DefineSetupFnComponent < { foo : any } > > ( Comp5 )
1552
+
1534
1553
defineComponent (
1535
- ( _props : { msg : string } ) => {
1554
+ // @ts -expect-error the props type is required when a generic type is present
1555
+ < T , > ( _props ) => {
1536
1556
return ( ) => { }
1537
1557
} ,
1538
1558
{
1539
- props : {
1540
- // @ts -expect-error prop type mismatch
1541
- msg : Number ,
1542
- } ,
1559
+ props : [ ] ,
1543
1560
} ,
1544
1561
)
1545
1562
} )
0 commit comments