|
| 1 | +//// [tests/cases/compiler/thisIndependentInterfaceMembers.ts] //// |
| 2 | + |
| 3 | +//// [thisIndependentInterfaceMembers.ts] |
| 4 | +// The member reuse optimization must preserve real, outer and foreign `this`. |
| 5 | +namespace capturedOuterThis { |
| 6 | + class Owner { |
| 7 | + read() { |
| 8 | + type OuterMap = { readonly [K in "owner"]: this }; |
| 9 | + interface Base extends OuterMap {} |
| 10 | + interface Derived extends Base { readonly derived: "derived" } |
| 11 | + const value = null! as Derived; |
| 12 | + const exact: this = value.owner; |
| 13 | + // @ts-expect-error The owner is not the mapped property's interface. |
| 14 | + const wrong: Derived = value.owner; |
| 15 | + return exact; |
| 16 | + } |
| 17 | + } |
| 18 | + class SpecialOwner extends Owner { readonly special = "special" } |
| 19 | + declare const owner: SpecialOwner; |
| 20 | + const exactOwner: SpecialOwner = owner.read(); |
| 21 | +} |
| 22 | + |
| 23 | +namespace fluentAlias { |
| 24 | + interface Fluent { self(): this } |
| 25 | + type FluentAlias = Fluent; |
| 26 | + type Marker = { readonly [K in "marker"]: "marker" }; |
| 27 | + interface Mixed extends Marker, FluentAlias {} |
| 28 | + interface Derived extends Mixed { readonly derived: "derived" } |
| 29 | + declare const value: Derived; |
| 30 | + const exact: Derived = value.self(); |
| 31 | +} |
| 32 | + |
| 33 | +namespace foreignMappedThis { |
| 34 | + interface Foreign { self(): this; readonly foreign: "foreign" } |
| 35 | + type Copy<T> = { readonly [K in keyof T]: T[K] }; |
| 36 | + interface Copied extends Copy<Foreign> {} |
| 37 | + interface Derived extends Copied { readonly derived: "derived" } |
| 38 | + declare const value: Derived; |
| 39 | + const exact: Foreign = value.self(); |
| 40 | + // @ts-expect-error A concrete map does not rebind the foreign method's this. |
| 41 | + const wrong: Derived = value.self(); |
| 42 | +} |
| 43 | + |
| 44 | +namespace mergedDeclarations { |
| 45 | + type Marker = { readonly [K in "marker"]: "marker" }; |
| 46 | + interface Merged extends Marker {} |
| 47 | + interface Merged { self(): this } |
| 48 | + interface Derived extends Merged { readonly derived: "derived" } |
| 49 | + declare const value: Derived; |
| 50 | + const exact: Derived = value.self(); |
| 51 | + const marker: "marker" = value.marker; |
| 52 | + |
| 53 | + interface Plain extends Marker {} |
| 54 | + interface Plain { readonly merged: "merged" } |
| 55 | + interface PlainDerived extends Plain { readonly derived: "derived" } |
| 56 | + declare const plain: PlainDerived; |
| 57 | + const inherited: "merged" = plain.merged; |
| 58 | +} |
| 59 | + |
| 60 | +namespace genericInterfaces { |
| 61 | + type Marker<T> = { readonly [K in "value"]: T }; |
| 62 | + interface Generic<T> extends Marker<T> { self(): this } |
| 63 | + interface NumberValue extends Generic<number> { readonly numeric: "numeric" } |
| 64 | + interface StringValue extends Generic<string> { readonly textual: "textual" } |
| 65 | + declare const numeric: NumberValue; |
| 66 | + declare const textual: StringValue; |
| 67 | + const numberValue: number = numeric.value; |
| 68 | + const stringValue: string = textual.value; |
| 69 | + const exact: NumberValue = numeric.self(); |
| 70 | + // @ts-expect-error Distinct type arguments may not share member substitutions. |
| 71 | + const wrong: number = textual.value; |
| 72 | + |
| 73 | + function enclosing<T>(input: T) { |
| 74 | + type OuterMap = { readonly [K in "value"]: T }; |
| 75 | + interface Local extends OuterMap {} |
| 76 | + interface Derived extends Local { readonly local: "local" } |
| 77 | + const value = null! as Derived; |
| 78 | + const exact: T = value.value; |
| 79 | + return { input, exact }; |
| 80 | + } |
| 81 | + const numericOuter: number = enclosing(1).exact; |
| 82 | + const stringOuter: string = enclosing("text").exact; |
| 83 | + // @ts-expect-error An enclosing generic remains specific to the invocation. |
| 84 | + const wrongOuter: number = enclosing("text").exact; |
| 85 | +} |
| 86 | + |
| 87 | +namespace mappedAliasArguments { |
| 88 | + type Value<T> = { readonly [K in "value"]: T }; |
| 89 | + type Alias<T> = Value<T>; |
| 90 | + interface Generic<T> extends Alias<T> {} |
| 91 | + interface NumberValue extends Generic<number> {} |
| 92 | + interface StringValue extends Generic<string> {} |
| 93 | + declare const numeric: NumberValue; |
| 94 | + declare const textual: StringValue; |
| 95 | + const numberValue: number = numeric.value; |
| 96 | + const stringValue: string = textual.value; |
| 97 | + // @ts-expect-error A mapped alias must retain its variable-containing argument. |
| 98 | + const wrong: number = textual.value; |
| 99 | + |
| 100 | + interface Foreign { self(): this; readonly foreign: "foreign" } |
| 101 | + type Copy<T> = { readonly [K in keyof T]: T[K] }; |
| 102 | + type ConcreteAlias = Copy<Foreign>; |
| 103 | + interface Copied extends ConcreteAlias {} |
| 104 | + interface Derived extends Copied { readonly derived: "derived" } |
| 105 | + declare const derived: Derived; |
| 106 | + const foreign: Foreign = derived.self(); |
| 107 | + // @ts-expect-error Mapping must preserve foreign-this even behind an alias. |
| 108 | + const notDerived: Derived = derived.self(); |
| 109 | +} |
| 110 | + |
| 111 | +namespace recursiveMembers { |
| 112 | + type Links = { readonly [K in "next"]: Node }; |
| 113 | + interface Node extends Links { readonly value: number } |
| 114 | + interface Derived extends Node { readonly own: string } |
| 115 | + declare const value: Derived; |
| 116 | + const next: Node = value.next; |
| 117 | + const nested: number = value.next.next.value; |
| 118 | + // @ts-expect-error Recursive members retain the declared Node, not Derived. |
| 119 | + const wrong: Derived = value.next; |
| 120 | + |
| 121 | + // This forces lookup of a mapped member through a recursively referenced type. |
| 122 | + type Reenter<T> = { readonly [K in "item"]: T }; |
| 123 | + interface Recursive extends Reenter<Recursive> { readonly own: "own" } |
| 124 | + interface RecursiveDerived extends Recursive { readonly more: "more" } |
| 125 | + declare const recursive: RecursiveDerived; |
| 126 | + const own: "own" = recursive.item.item.own; |
| 127 | + // @ts-expect-error A re-entrant lookup does not substitute a descendant. |
| 128 | + const wrongRecursive: RecursiveDerived = recursive.item; |
| 129 | +} |
| 130 | + |
| 131 | +namespace rejectedBases { |
| 132 | + type LeftMap = { readonly [K in "value"]: number }; |
| 133 | + type RightMap = { readonly [K in "value"]: string }; |
| 134 | + interface Left extends LeftMap {} |
| 135 | + interface Right extends RightMap {} |
| 136 | + // @ts-expect-error Conflicting inherited properties must still reject. |
| 137 | + interface Conflict extends Left, Right {} |
| 138 | + |
| 139 | + // @ts-expect-error A circular mapped constraint remains an error. |
| 140 | + type Reentered = { [K in keyof Circular]: Circular[K] }; |
| 141 | + // @ts-expect-error Resolution placeholders must not prove valid ancestry. |
| 142 | + interface Circular extends Reentered { readonly own: string } |
| 143 | + |
| 144 | + // @ts-expect-error An unresolved mapped key still reports its error. |
| 145 | + type ErrorMap = { [K in MissingKey]: number }; |
| 146 | + interface ErrorBase extends ErrorMap {} |
| 147 | + // @ts-expect-error The erroneous key must not erase the numeric index contract. |
| 148 | + interface ErrorDerived extends ErrorBase { readonly own: "own" } |
| 149 | +} |
| 150 | + |
| 151 | +namespace concreteMappedMembers { |
| 152 | + type Value<T> = { readonly [K in "value"]: T }; |
| 153 | + interface Plain extends Value<number> {} |
| 154 | + interface Derived extends Plain { readonly own: "own" } |
| 155 | + declare const value: Derived; |
| 156 | + const exact: number = value.value; |
| 157 | + // @ts-expect-error Concrete mapped properties keep their exact type. |
| 158 | + const wrong: string = value.value; |
| 159 | + |
| 160 | + interface WithIndex { readonly [key: string]: number } |
| 161 | + interface IndexBase extends WithIndex {} |
| 162 | + interface IndexDerived extends IndexBase { readonly own: 1 } |
| 163 | + declare const indexed: IndexDerived; |
| 164 | + const entry: number = indexed["arbitrary"]; |
| 165 | + |
| 166 | + type Callable = { (): number; readonly [K: string]: unknown }; |
| 167 | + interface CallBase extends Callable {} |
| 168 | + interface CallDerived extends CallBase { readonly own: "own" } |
| 169 | + declare const callable: CallDerived; |
| 170 | + const result: number = callable(); |
| 171 | +} |
| 172 | + |
| 173 | + |
| 174 | +//// [thisIndependentInterfaceMembers.js] |
| 175 | +"use strict"; |
| 176 | +// The member reuse optimization must preserve real, outer and foreign `this`. |
| 177 | +var capturedOuterThis; |
| 178 | +(function (capturedOuterThis) { |
| 179 | + class Owner { |
| 180 | + read() { |
| 181 | + const value = null; |
| 182 | + const exact = value.owner; |
| 183 | + // @ts-expect-error The owner is not the mapped property's interface. |
| 184 | + const wrong = value.owner; |
| 185 | + return exact; |
| 186 | + } |
| 187 | + } |
| 188 | + class SpecialOwner extends Owner { |
| 189 | + special = "special"; |
| 190 | + } |
| 191 | + const exactOwner = owner.read(); |
| 192 | +})(capturedOuterThis || (capturedOuterThis = {})); |
| 193 | +var fluentAlias; |
| 194 | +(function (fluentAlias) { |
| 195 | + const exact = value.self(); |
| 196 | +})(fluentAlias || (fluentAlias = {})); |
| 197 | +var foreignMappedThis; |
| 198 | +(function (foreignMappedThis) { |
| 199 | + const exact = value.self(); |
| 200 | + // @ts-expect-error A concrete map does not rebind the foreign method's this. |
| 201 | + const wrong = value.self(); |
| 202 | +})(foreignMappedThis || (foreignMappedThis = {})); |
| 203 | +var mergedDeclarations; |
| 204 | +(function (mergedDeclarations) { |
| 205 | + const exact = value.self(); |
| 206 | + const marker = value.marker; |
| 207 | + const inherited = plain.merged; |
| 208 | +})(mergedDeclarations || (mergedDeclarations = {})); |
| 209 | +var genericInterfaces; |
| 210 | +(function (genericInterfaces) { |
| 211 | + const numberValue = numeric.value; |
| 212 | + const stringValue = textual.value; |
| 213 | + const exact = numeric.self(); |
| 214 | + // @ts-expect-error Distinct type arguments may not share member substitutions. |
| 215 | + const wrong = textual.value; |
| 216 | + function enclosing(input) { |
| 217 | + const value = null; |
| 218 | + const exact = value.value; |
| 219 | + return { input, exact }; |
| 220 | + } |
| 221 | + const numericOuter = enclosing(1).exact; |
| 222 | + const stringOuter = enclosing("text").exact; |
| 223 | + // @ts-expect-error An enclosing generic remains specific to the invocation. |
| 224 | + const wrongOuter = enclosing("text").exact; |
| 225 | +})(genericInterfaces || (genericInterfaces = {})); |
| 226 | +var mappedAliasArguments; |
| 227 | +(function (mappedAliasArguments) { |
| 228 | + const numberValue = numeric.value; |
| 229 | + const stringValue = textual.value; |
| 230 | + // @ts-expect-error A mapped alias must retain its variable-containing argument. |
| 231 | + const wrong = textual.value; |
| 232 | + const foreign = derived.self(); |
| 233 | + // @ts-expect-error Mapping must preserve foreign-this even behind an alias. |
| 234 | + const notDerived = derived.self(); |
| 235 | +})(mappedAliasArguments || (mappedAliasArguments = {})); |
| 236 | +var recursiveMembers; |
| 237 | +(function (recursiveMembers) { |
| 238 | + const next = value.next; |
| 239 | + const nested = value.next.next.value; |
| 240 | + // @ts-expect-error Recursive members retain the declared Node, not Derived. |
| 241 | + const wrong = value.next; |
| 242 | + const own = recursive.item.item.own; |
| 243 | + // @ts-expect-error A re-entrant lookup does not substitute a descendant. |
| 244 | + const wrongRecursive = recursive.item; |
| 245 | +})(recursiveMembers || (recursiveMembers = {})); |
| 246 | +var concreteMappedMembers; |
| 247 | +(function (concreteMappedMembers) { |
| 248 | + const exact = value.value; |
| 249 | + // @ts-expect-error Concrete mapped properties keep their exact type. |
| 250 | + const wrong = value.value; |
| 251 | + const entry = indexed["arbitrary"]; |
| 252 | + const result = callable(); |
| 253 | +})(concreteMappedMembers || (concreteMappedMembers = {})); |
0 commit comments