|
| 1 | +//// [tests/cases/compiler/contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts] //// |
| 2 | + |
| 3 | +=== contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts === |
| 4 | +// based on https://github.com/microsoft/TypeScript/issues/51831 |
| 5 | + |
| 6 | +type NoMatchAll<K> = Exclude<K, "_"> |
| 7 | +>NoMatchAll : Symbol(NoMatchAll, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 0, 0)) |
| 8 | +>K : Symbol(K, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 2, 16)) |
| 9 | +>Exclude : Symbol(Exclude, Decl(lib.es5.d.ts, --, --)) |
| 10 | +>K : Symbol(K, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 2, 16)) |
| 11 | + |
| 12 | +type Union<T extends object> = { |
| 13 | +>Union : Symbol(Union, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 2, 36)) |
| 14 | +>T : Symbol(T, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 3, 11)) |
| 15 | + |
| 16 | + [P in keyof T]: ({ [Q in "kind"]: P } & T[P]) extends infer U ? { [Q in keyof U]: U[Q] } : never |
| 17 | +>P : Symbol(P, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 4, 3)) |
| 18 | +>T : Symbol(T, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 3, 11)) |
| 19 | +>Q : Symbol(Q, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 4, 22)) |
| 20 | +>P : Symbol(P, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 4, 3)) |
| 21 | +>T : Symbol(T, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 3, 11)) |
| 22 | +>P : Symbol(P, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 4, 3)) |
| 23 | +>U : Symbol(U, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 4, 61)) |
| 24 | +>Q : Symbol(Q, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 4, 69)) |
| 25 | +>U : Symbol(U, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 4, 61)) |
| 26 | +>U : Symbol(U, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 4, 61)) |
| 27 | +>Q : Symbol(Q, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 4, 69)) |
| 28 | + |
| 29 | +}[keyof T] |
| 30 | +>T : Symbol(T, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 3, 11)) |
| 31 | + |
| 32 | +type UnionMap<U extends { kind: string }> = { [K in U["kind"]]: U extends { kind: K } ? U : never } |
| 33 | +>UnionMap : Symbol(UnionMap, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 5, 10)) |
| 34 | +>U : Symbol(U, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 6, 14)) |
| 35 | +>kind : Symbol(kind, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 6, 25)) |
| 36 | +>K : Symbol(K, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 6, 47)) |
| 37 | +>U : Symbol(U, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 6, 14)) |
| 38 | +>U : Symbol(U, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 6, 14)) |
| 39 | +>kind : Symbol(kind, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 6, 75)) |
| 40 | +>K : Symbol(K, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 6, 47)) |
| 41 | +>U : Symbol(U, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 6, 14)) |
| 42 | + |
| 43 | +type ExhaustivePattern<T extends { kind: string }, R> = { [K in T["kind"] as NoMatchAll<K>]: (union: UnionMap<T>[K]) => R }; |
| 44 | +>ExhaustivePattern : Symbol(ExhaustivePattern, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 6, 99)) |
| 45 | +>T : Symbol(T, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 7, 23)) |
| 46 | +>kind : Symbol(kind, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 7, 34)) |
| 47 | +>R : Symbol(R, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 7, 50)) |
| 48 | +>K : Symbol(K, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 7, 59)) |
| 49 | +>T : Symbol(T, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 7, 23)) |
| 50 | +>NoMatchAll : Symbol(NoMatchAll, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 0, 0)) |
| 51 | +>K : Symbol(K, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 7, 59)) |
| 52 | +>union : Symbol(union, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 7, 94)) |
| 53 | +>UnionMap : Symbol(UnionMap, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 5, 10)) |
| 54 | +>T : Symbol(T, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 7, 23)) |
| 55 | +>K : Symbol(K, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 7, 59)) |
| 56 | +>R : Symbol(R, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 7, 50)) |
| 57 | + |
| 58 | +type NonExhaustivePattern<T extends { kind: string }, R> = { [K in T["kind"] as NoMatchAll<K>]?: (union: UnionMap<T>[K]) => R } & {_: (union: T) => R}; |
| 59 | +>NonExhaustivePattern : Symbol(NonExhaustivePattern, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 7, 124)) |
| 60 | +>T : Symbol(T, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 8, 26)) |
| 61 | +>kind : Symbol(kind, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 8, 37)) |
| 62 | +>R : Symbol(R, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 8, 53)) |
| 63 | +>K : Symbol(K, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 8, 62)) |
| 64 | +>T : Symbol(T, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 8, 26)) |
| 65 | +>NoMatchAll : Symbol(NoMatchAll, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 0, 0)) |
| 66 | +>K : Symbol(K, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 8, 62)) |
| 67 | +>union : Symbol(union, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 8, 98)) |
| 68 | +>UnionMap : Symbol(UnionMap, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 5, 10)) |
| 69 | +>T : Symbol(T, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 8, 26)) |
| 70 | +>K : Symbol(K, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 8, 62)) |
| 71 | +>R : Symbol(R, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 8, 53)) |
| 72 | +>_ : Symbol(_, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 8, 131)) |
| 73 | +>union : Symbol(union, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 8, 135)) |
| 74 | +>T : Symbol(T, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 8, 26)) |
| 75 | +>R : Symbol(R, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 8, 53)) |
| 76 | + |
| 77 | +type Pattern<T extends { kind: string }, R> = ExhaustivePattern<T, R> | NonExhaustivePattern<T, R>; |
| 78 | +>Pattern : Symbol(Pattern, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 8, 151)) |
| 79 | +>T : Symbol(T, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 9, 13)) |
| 80 | +>kind : Symbol(kind, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 9, 24)) |
| 81 | +>R : Symbol(R, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 9, 40)) |
| 82 | +>ExhaustivePattern : Symbol(ExhaustivePattern, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 6, 99)) |
| 83 | +>T : Symbol(T, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 9, 13)) |
| 84 | +>R : Symbol(R, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 9, 40)) |
| 85 | +>NonExhaustivePattern : Symbol(NonExhaustivePattern, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 7, 124)) |
| 86 | +>T : Symbol(T, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 9, 13)) |
| 87 | +>R : Symbol(R, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 9, 40)) |
| 88 | + |
| 89 | +function match<U extends { kind: string }, T>(union: U, pattern: Pattern<U, T>): T { |
| 90 | +>match : Symbol(match, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 9, 99)) |
| 91 | +>U : Symbol(U, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 11, 15)) |
| 92 | +>kind : Symbol(kind, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 11, 26)) |
| 93 | +>T : Symbol(T, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 11, 42)) |
| 94 | +>union : Symbol(union, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 11, 46)) |
| 95 | +>U : Symbol(U, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 11, 15)) |
| 96 | +>pattern : Symbol(pattern, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 11, 55)) |
| 97 | +>Pattern : Symbol(Pattern, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 8, 151)) |
| 98 | +>U : Symbol(U, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 11, 15)) |
| 99 | +>T : Symbol(T, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 11, 42)) |
| 100 | +>T : Symbol(T, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 11, 42)) |
| 101 | + |
| 102 | + if((pattern as any)[union.kind]) { |
| 103 | +>pattern : Symbol(pattern, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 11, 55)) |
| 104 | +>union.kind : Symbol(kind, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 11, 26)) |
| 105 | +>union : Symbol(union, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 11, 46)) |
| 106 | +>kind : Symbol(kind, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 11, 26)) |
| 107 | + |
| 108 | + return (pattern as any)[union.kind](union as U) as T |
| 109 | +>pattern : Symbol(pattern, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 11, 55)) |
| 110 | +>union.kind : Symbol(kind, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 11, 26)) |
| 111 | +>union : Symbol(union, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 11, 46)) |
| 112 | +>kind : Symbol(kind, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 11, 26)) |
| 113 | +>union : Symbol(union, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 11, 46)) |
| 114 | +>U : Symbol(U, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 11, 15)) |
| 115 | +>T : Symbol(T, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 11, 42)) |
| 116 | + } |
| 117 | + return (pattern as any)["_"](union as U) as T |
| 118 | +>pattern : Symbol(pattern, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 11, 55)) |
| 119 | +>union : Symbol(union, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 11, 46)) |
| 120 | +>U : Symbol(U, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 11, 15)) |
| 121 | +>T : Symbol(T, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 11, 42)) |
| 122 | +} |
| 123 | + |
| 124 | +type ValueType = Union<{ |
| 125 | +>ValueType : Symbol(ValueType, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 16, 1)) |
| 126 | +>Union : Symbol(Union, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 2, 36)) |
| 127 | + |
| 128 | + String: {value: string}, |
| 129 | +>String : Symbol(String, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 18, 24)) |
| 130 | +>value : Symbol(value, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 19, 11)) |
| 131 | + |
| 132 | + Number: {value: number}, |
| 133 | +>Number : Symbol(Number, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 19, 26)) |
| 134 | +>value : Symbol(value, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 20, 11)) |
| 135 | + |
| 136 | + Boolean: {value: boolean}, |
| 137 | +>Boolean : Symbol(Boolean, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 20, 26)) |
| 138 | +>value : Symbol(value, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 21, 12)) |
| 139 | + |
| 140 | + Date: {value: Date} |
| 141 | +>Date : Symbol(Date, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 21, 28)) |
| 142 | +>value : Symbol(value, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 22, 9)) |
| 143 | +>Date : Symbol(Date, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.scripthost.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --)) |
| 144 | + |
| 145 | +}> |
| 146 | + |
| 147 | +function main(value: ValueType) { |
| 148 | +>main : Symbol(main, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 23, 2)) |
| 149 | +>value : Symbol(value, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 25, 14)) |
| 150 | +>ValueType : Symbol(ValueType, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 16, 1)) |
| 151 | + |
| 152 | + let test1 = match<ValueType, string>(value, { |
| 153 | +>test1 : Symbol(test1, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 26, 5)) |
| 154 | +>match : Symbol(match, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 9, 99)) |
| 155 | +>ValueType : Symbol(ValueType, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 16, 1)) |
| 156 | +>value : Symbol(value, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 25, 14)) |
| 157 | + |
| 158 | + String: ({value}) => value, |
| 159 | +>String : Symbol(String, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 26, 47)) |
| 160 | +>value : Symbol(value, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 27, 14)) |
| 161 | +>value : Symbol(value, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 27, 14)) |
| 162 | + |
| 163 | + Number: ({value}) => value.toString(), |
| 164 | +>Number : Symbol(Number, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 27, 31)) |
| 165 | +>value : Symbol(value, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 28, 14)) |
| 166 | +>value.toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) |
| 167 | +>value : Symbol(value, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 28, 14)) |
| 168 | +>toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) |
| 169 | + |
| 170 | + _: (token) => "Unknown" |
| 171 | +>_ : Symbol(_, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 28, 42)) |
| 172 | +>token : Symbol(token, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 29, 8)) |
| 173 | + |
| 174 | + }); |
| 175 | + |
| 176 | + let test2 = match(value, { |
| 177 | +>test2 : Symbol(test2, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 32, 5)) |
| 178 | +>match : Symbol(match, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 9, 99)) |
| 179 | +>value : Symbol(value, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 25, 14)) |
| 180 | + |
| 181 | + String: ({value}) => value, |
| 182 | +>String : Symbol(String, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 32, 28)) |
| 183 | +>value : Symbol(value, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 33, 14)) |
| 184 | +>value : Symbol(value, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 33, 14)) |
| 185 | + |
| 186 | + Number: ({value}) => value.toString(), |
| 187 | +>Number : Symbol(Number, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 33, 31)) |
| 188 | +>value : Symbol(value, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 34, 14)) |
| 189 | +>value.toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) |
| 190 | +>value : Symbol(value, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 34, 14)) |
| 191 | +>toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) |
| 192 | + |
| 193 | + _: (token) => "Unknown" |
| 194 | +>_ : Symbol(_, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 34, 42)) |
| 195 | +>token : Symbol(token, Decl(contextualTypesNegatedTypeLikeConstraintInGenericMappedType4.ts, 35, 8)) |
| 196 | + |
| 197 | + }); |
| 198 | +} |
| 199 | + |
0 commit comments