kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/typescript/testdata/generic_tvar.ts (about) 1 export {} 2 3 // Check references to a type in a generic. 4 5 //- @IFace defines/binding IFace 6 //- IFace.node/kind interface 7 interface IFace { 8 foo: string; 9 } 10 11 // Reference to IFace in a generic type. 12 //- @IFace ref IFace 13 let x: Map<string, IFace>; 14 // Reference to IFace in an expression. 15 //- @IFace ref IFace 16 x = new Map<string, IFace>(); 17 18 // Create a generic type and instantiate it. 19 //- @Container defines/binding Container 20 //- @T defines/binding ContainerT 21 //- ContainerT.node/kind tvar 22 //- Container tparam.0 ContainerT 23 interface Container<T> { 24 //- @T ref ContainerT 25 //- !{@T ref Container} 26 contained: T; 27 } 28 let box: Container<IFace>; 29 30 //- @#0T defines/binding FnT 31 //- FnT.node/kind tvar 32 //- @generic defines/binding FnGeneric 33 //- FnGeneric tparam.0 FnT 34 //- @IFace ref IFace 35 function generic<T>(x: T, y: IFace) { 36 return x; 37 } 38 39 // Simple constrained generic. 40 //- @T defines/binding ConstrainedGenericT 41 //- @IFace ref IFace 42 //- ConstrainedGenericT.node/kind tvar 43 //- ConstrainedGenericT bounded/upper IFace 44 function constrainedGeneric<T extends IFace>() {} 45 46 // Constrained generic with reference to type param within it. 47 //- @constrainedGenericRef defines/binding FnconstrainedGenericRef 48 //- FnconstrainedGenericRef tparam.0 ConstrainedGenericT2 49 //- FnconstrainedGenericRef tparam.1 ConstrainedGenericT2K 50 //- @#0T defines/binding ConstrainedGenericT2 51 //- !{@#0T ref ConstrainedGenericT} 52 //- @#1T ref ConstrainedGenericT2 53 //- @#0K defines/binding ConstrainedGenericT2K 54 //- @#1K ref ConstrainedGenericT2K 55 function constrainedGenericRef<T, K extends keyof T>(k: K) {} 56 57 // Recursive generic type. 58 //- @constrainedGenericRecursive defines/binding FncGR 59 //- FncGR tparam.0 ConstrainedGenericT3 60 //- @#0T defines/binding ConstrainedGenericT3 61 //- @#1T ref ConstrainedGenericT3 62 function constrainedGenericRecursive<T extends Array<T>>() {} 63 64 // Default generic. 65 //- @defaultGeneric defines/binding FndefaultGeneric 66 //- FndefaultGeneric tparam.0 DefaultGeneric 67 //- @#0T defines/binding DefaultGeneric 68 //- @#1T ref DefaultGeneric 69 //- @IFace ref IFace 70 function defaultGeneric<T = IFace>(t: T) {} 71 72 // Default generic with extends. 73 //- @defaultGeneric2 defines/binding FndefaultGeneric2 74 //- FndefaultGeneric2 tparam.0 DefaultGeneric2 75 //- @#0T defines/binding DefaultGeneric2 76 //- @#1T ref DefaultGeneric2 77 //- @#0IFace ref IFace 78 //- @#1IFace ref IFace 79 function defaultGeneric2<T extends IFace = IFace>(t: T) {} 80 81 //- @Container ref Container 82 //- @IFace ref Iface 83 interface ExtendsGeneric extends Container<IFace> {} 84 85 //- @Container ref Container 86 //- @IFace ref Iface 87 class ImplementsGeneric implements Container<IFace> { 88 //- @IFace ref Iface 89 contained: IFace; 90 }