|
| 1 | +//// [file.js] |
| 2 | +/** |
| 3 | + * @template {string | number} [T=string] - ok: defaults are permitted |
| 4 | + * @typedef {[T]} A |
| 5 | + */ |
| 6 | + |
| 7 | +/** @type {A} */ // ok, default for `T` in `A` is `string` |
| 8 | +const aDefault1 = [""]; |
| 9 | +/** @type {A} */ // error: `number` is not assignable to string` |
| 10 | +const aDefault2 = [0]; |
| 11 | +/** @type {A<string>} */ // ok, `T` is provided for `A` |
| 12 | +const aString = [""]; |
| 13 | +/** @type {A<number>} */ // ok, `T` is provided for `A` |
| 14 | +const aNumber = [0]; |
| 15 | + |
| 16 | +/** |
| 17 | + * @template T |
| 18 | + * @template [U=T] - ok: default can reference earlier type parameter |
| 19 | + * @typedef {[T, U]} B |
| 20 | + */ |
| 21 | + |
| 22 | +/** |
| 23 | + * @template {string | number} [T] - error: default requires an `=type` |
| 24 | + * @typedef {[T]} C |
| 25 | + */ |
| 26 | + |
| 27 | +/** |
| 28 | + * @template {string | number} [T=] - error: default requires a `type` |
| 29 | + * @typedef {[T]} D |
| 30 | + */ |
| 31 | + |
| 32 | +/** |
| 33 | + * @template {string | number} [T=string] |
| 34 | + * @template U - error: Required type parameters cannot follow optional type parameters |
| 35 | + * @typedef {[T, U]} E |
| 36 | + */ |
| 37 | + |
| 38 | +/** |
| 39 | + * @template [T=U] - error: Type parameter defaults can only reference previously declared type parameters. |
| 40 | + * @template [U=T] |
| 41 | + * @typedef {[T, U]} G |
| 42 | + */ |
| 43 | + |
| 44 | +/** |
| 45 | + * @template T |
| 46 | + * @template [U=T] - ok: default can reference earlier type parameter |
| 47 | + * @param {T} a |
| 48 | + * @param {U} b |
| 49 | + */ |
| 50 | +function f1(a, b) {} |
| 51 | + |
| 52 | + /** |
| 53 | + * @template {string | number} [T=string] |
| 54 | + * @template U - error: Required type parameters cannot follow optional type parameters |
| 55 | + * @param {T} a |
| 56 | + * @param {U} b |
| 57 | + */ |
| 58 | +function f2(a, b) {} |
| 59 | + |
| 60 | +/** |
| 61 | + * @template [T=U] - error: Type parameter defaults can only reference previously declared type parameters. |
| 62 | + * @template [U=T] |
| 63 | + * @param {T} a |
| 64 | + * @param {U} b |
| 65 | + */ |
| 66 | +function f3(a, b) {} |
| 67 | + |
| 68 | + |
| 69 | +//// [file.js] |
| 70 | +/** |
| 71 | + * @template {string | number} [T=string] - ok: defaults are permitted |
| 72 | + * @typedef {[T]} A |
| 73 | + */ |
| 74 | +/** @type {A} */ // ok, default for `T` in `A` is `string` |
| 75 | +var aDefault1 = [""]; |
| 76 | +/** @type {A} */ // error: `number` is not assignable to string` |
| 77 | +var aDefault2 = [0]; |
| 78 | +/** @type {A<string>} */ // ok, `T` is provided for `A` |
| 79 | +var aString = [""]; |
| 80 | +/** @type {A<number>} */ // ok, `T` is provided for `A` |
| 81 | +var aNumber = [0]; |
| 82 | +/** |
| 83 | + * @template T |
| 84 | + * @template [U=T] - ok: default can reference earlier type parameter |
| 85 | + * @typedef {[T, U]} B |
| 86 | + */ |
| 87 | +/** |
| 88 | + * @template {string | number} [T] - error: default requires an `=type` |
| 89 | + * @typedef {[T]} C |
| 90 | + */ |
| 91 | +/** |
| 92 | + * @template {string | number} [T=] - error: default requires a `type` |
| 93 | + * @typedef {[T]} D |
| 94 | + */ |
| 95 | +/** |
| 96 | + * @template {string | number} [T=string] |
| 97 | + * @template U - error: Required type parameters cannot follow optional type parameters |
| 98 | + * @typedef {[T, U]} E |
| 99 | + */ |
| 100 | +/** |
| 101 | + * @template [T=U] - error: Type parameter defaults can only reference previously declared type parameters. |
| 102 | + * @template [U=T] |
| 103 | + * @typedef {[T, U]} G |
| 104 | + */ |
| 105 | +/** |
| 106 | + * @template T |
| 107 | + * @template [U=T] - ok: default can reference earlier type parameter |
| 108 | + * @param {T} a |
| 109 | + * @param {U} b |
| 110 | + */ |
| 111 | +function f1(a, b) { } |
| 112 | +/** |
| 113 | +* @template {string | number} [T=string] |
| 114 | +* @template U - error: Required type parameters cannot follow optional type parameters |
| 115 | +* @param {T} a |
| 116 | +* @param {U} b |
| 117 | +*/ |
| 118 | +function f2(a, b) { } |
| 119 | +/** |
| 120 | + * @template [T=U] - error: Type parameter defaults can only reference previously declared type parameters. |
| 121 | + * @template [U=T] |
| 122 | + * @param {T} a |
| 123 | + * @param {U} b |
| 124 | + */ |
| 125 | +function f3(a, b) { } |
| 126 | + |
| 127 | + |
| 128 | +//// [file.d.ts] |
| 129 | +/** |
| 130 | + * @template T |
| 131 | + * @template [U=T] - ok: default can reference earlier type parameter |
| 132 | + * @typedef {[T, U]} B |
| 133 | + */ |
| 134 | +/** |
| 135 | + * @template {string | number} [T] - error: default requires an `=type` |
| 136 | + * @typedef {[T]} C |
| 137 | + */ |
| 138 | +/** |
| 139 | + * @template {string | number} [T=] - error: default requires a `type` |
| 140 | + * @typedef {[T]} D |
| 141 | + */ |
| 142 | +/** |
| 143 | + * @template {string | number} [T=string] |
| 144 | + * @template U - error: Required type parameters cannot follow optional type parameters |
| 145 | + * @typedef {[T, U]} E |
| 146 | + */ |
| 147 | +/** |
| 148 | + * @template [T=U] - error: Type parameter defaults can only reference previously declared type parameters. |
| 149 | + * @template [U=T] |
| 150 | + * @typedef {[T, U]} G |
| 151 | + */ |
| 152 | +/** |
| 153 | + * @template T |
| 154 | + * @template [U=T] - ok: default can reference earlier type parameter |
| 155 | + * @param {T} a |
| 156 | + * @param {U} b |
| 157 | + */ |
| 158 | +declare function f1<T, U = T>(a: T, b: U): void; |
| 159 | +/** |
| 160 | +* @template {string | number} [T=string] |
| 161 | +* @template U - error: Required type parameters cannot follow optional type parameters |
| 162 | +* @param {T} a |
| 163 | +* @param {U} b |
| 164 | +*/ |
| 165 | +declare function f2<T extends string | number = string, U>(a: T, b: U): void; |
| 166 | +/** |
| 167 | + * @template [T=U] - error: Type parameter defaults can only reference previously declared type parameters. |
| 168 | + * @template [U=T] |
| 169 | + * @param {T} a |
| 170 | + * @param {U} b |
| 171 | + */ |
| 172 | +declare function f3<T = U, U = T>(a: T, b: U): void; |
| 173 | +/** |
| 174 | + * @template {string | number} [T=string] - ok: defaults are permitted |
| 175 | + * @typedef {[T]} A |
| 176 | + */ |
| 177 | +/** @type {A} */ declare const aDefault1: A<string>; |
| 178 | +/** @type {A} */ declare const aDefault2: A<string>; |
| 179 | +/** @type {A<string>} */ declare const aString: A<string>; |
| 180 | +/** @type {A<number>} */ declare const aNumber: A<number>; |
| 181 | +type B<T, U = T> = [T, U]; |
| 182 | +type C<T extends string | number = any> = [T]; |
| 183 | +type D<T extends string | number = any> = [T]; |
| 184 | +type E<T extends string | number = string, U> = [T, U]; |
| 185 | +type G<T = U, U = T> = [T, U]; |
| 186 | +type A<T extends string | number = string> = [T]; |
0 commit comments