@@ -119,6 +119,7 @@ import {
119
119
TupleTypeReference ,
120
120
Type ,
121
121
TypeFlags ,
122
+ TypePredicate ,
122
123
unescapeLeadingUnderscores ,
123
124
UserPreferences ,
124
125
usingSingleLineStringWriter ,
@@ -405,6 +406,16 @@ export function provideInlayHints(context: InlayHintsContext): InlayHint[] {
405
406
return ;
406
407
}
407
408
409
+ const typePredicate = checker . getTypePredicateOfSignature ( signature ) ;
410
+
411
+ if ( typePredicate ?. type ) {
412
+ const hintParts = typePredicateToInlayHintParts ( typePredicate ) ;
413
+ if ( hintParts ) {
414
+ addTypeHints ( hintParts , getTypeAnnotationPosition ( decl ) ) ;
415
+ return ;
416
+ }
417
+ }
418
+
408
419
const returnType = checker . getReturnTypeOfSignature ( signature ) ;
409
420
if ( isModuleReferenceType ( returnType ) ) {
410
421
return ;
@@ -474,17 +485,42 @@ export function provideInlayHints(context: InlayHintsContext): InlayHint[] {
474
485
} ) ;
475
486
}
476
487
488
+ function printTypePredicateInSingleLine ( typePredicate : TypePredicate ) {
489
+ const flags = NodeBuilderFlags . IgnoreErrors | NodeBuilderFlags . AllowUniqueESSymbolType | NodeBuilderFlags . UseAliasDefinedOutsideCurrentScope ;
490
+ const printer = createPrinterWithRemoveComments ( ) ;
491
+
492
+ return usingSingleLineStringWriter ( writer => {
493
+ const typePredicateNode = checker . typePredicateToTypePredicateNode ( typePredicate , /*enclosingDeclaration*/ undefined , flags ) ;
494
+ Debug . assertIsDefined ( typePredicateNode , "should always get typePredicateNode" ) ;
495
+ printer . writeNode ( EmitHint . Unspecified , typePredicateNode , /*sourceFile*/ file , writer ) ;
496
+ } ) ;
497
+ }
498
+
477
499
function typeToInlayHintParts ( type : Type ) : InlayHintDisplayPart [ ] | string {
478
500
if ( ! shouldUseInteractiveInlayHints ( preferences ) ) {
479
501
return printTypeInSingleLine ( type ) ;
480
502
}
481
503
482
504
const flags = NodeBuilderFlags . IgnoreErrors | NodeBuilderFlags . AllowUniqueESSymbolType | NodeBuilderFlags . UseAliasDefinedOutsideCurrentScope ;
483
505
const typeNode = checker . typeToTypeNode ( type , /*enclosingDeclaration*/ undefined , flags ) ;
506
+ Debug . assertIsDefined ( typeNode , "should always get typeNode" ) ;
507
+ return getInlayHintDisplayParts ( typeNode ) ;
508
+ }
509
+
510
+ function typePredicateToInlayHintParts ( typePredicate : TypePredicate ) : InlayHintDisplayPart [ ] | string {
511
+ if ( ! shouldUseInteractiveInlayHints ( preferences ) ) {
512
+ return printTypePredicateInSingleLine ( typePredicate ) ;
513
+ }
514
+
515
+ const flags = NodeBuilderFlags . IgnoreErrors | NodeBuilderFlags . AllowUniqueESSymbolType | NodeBuilderFlags . UseAliasDefinedOutsideCurrentScope ;
516
+ const typeNode = checker . typePredicateToTypePredicateNode ( typePredicate , /*enclosingDeclaration*/ undefined , flags ) ;
484
517
Debug . assertIsDefined ( typeNode , "should always get typenode" ) ;
518
+ return getInlayHintDisplayParts ( typeNode ) ;
519
+ }
485
520
521
+ function getInlayHintDisplayParts ( node : Node ) {
486
522
const parts : InlayHintDisplayPart [ ] = [ ] ;
487
- visitForDisplayParts ( typeNode ) ;
523
+ visitForDisplayParts ( node ) ;
488
524
return parts ;
489
525
490
526
function visitForDisplayParts ( node : Node ) {
0 commit comments