1// @strictNullChecks: true 2// @declaration: true 3 4function f1<T>(x: T, k: keyof T) { 5 return x[k]; 6} 7 8function f2<T, K extends keyof T>(x: T, k: K) { 9 return x[k]; 10} 11 12function f3<T, U extends T>(x: T, y: U, k: keyof T) { 13 x[k] = y[k]; 14 y[k] = x[k]; // Error 15} 16 17function f4<T, U extends T, K extends keyof T>(x: T, y: U, k: K) { 18 x[k] = y[k]; 19 y[k] = x[k]; // Error 20} 21 22function f5<T, U extends T>(x: T, y: U, k: keyof U) { 23 x[k] = y[k]; // Error 24 y[k] = x[k]; // Error 25} 26 27function f6<T, U extends T, K extends keyof U>(x: T, y: U, k: K) { 28 x[k] = y[k]; // Error 29 y[k] = x[k]; // Error 30} 31 32function f10<T>(x: T, y: Partial<T>, k: keyof T) { 33 x[k] = y[k]; // Error 34 y[k] = x[k]; 35} 36 37function f11<T, K extends keyof T>(x: T, y: Partial<T>, k: K) { 38 x[k] = y[k]; // Error 39 y[k] = x[k]; 40} 41 42function f12<T, U extends T>(x: T, y: Partial<U>, k: keyof T) { 43 x[k] = y[k]; // Error 44 y[k] = x[k]; // Error 45} 46 47function f13<T, U extends T, K extends keyof T>(x: T, y: Partial<U>, k: K) { 48 x[k] = y[k]; // Error 49 y[k] = x[k]; // Error 50} 51 52function f20<T>(x: T, y: Readonly<T>, k: keyof T) { 53 x[k] = y[k]; 54 y[k] = x[k]; // Error 55} 56 57function f21<T, K extends keyof T>(x: T, y: Readonly<T>, k: K) { 58 x[k] = y[k]; 59 y[k] = x[k]; // Error 60} 61 62function f22<T, U extends T>(x: T, y: Readonly<U>, k: keyof T) { 63 x[k] = y[k]; 64 y[k] = x[k]; // Error 65} 66 67function f23<T, U extends T, K extends keyof T>(x: T, y: Readonly<U>, k: K) { 68 x[k] = y[k]; 69 y[k] = x[k]; // Error 70} 71 72type Thing = { a: string, b: string }; 73 74function f30<T>(x: T, y: Partial<T>) { 75 x = y; // Error 76 y = x; 77} 78 79function f31<T extends Thing>(x: Partial<Thing>, y: Partial<T>) { 80 x = y; 81 y = x; // Error 82} 83 84function f40<T>(x: T, y: Readonly<T>) { 85 x = y; 86 y = x; 87} 88 89function f41<T extends Thing>(x: Readonly<Thing>, y: Readonly<T>) { 90 x = y; 91 y = x; // Error 92} 93 94type Item = { 95 name: string; 96} 97 98type ItemMap = { 99 [x: string]: Item; 100} 101 102function f50<T extends ItemMap>(obj: T, key: keyof T) { 103 let item: Item = obj[key]; 104 return obj[key].name; 105} 106 107function f51<T extends ItemMap, K extends keyof T>(obj: T, key: K) { 108 let item: Item = obj[key]; 109 return obj[key].name; 110} 111 112type T1<T> = { 113 [P in keyof T]: T[P]; 114} 115 116type T2<T> = { 117 [P in keyof T]: T[P]; 118} 119 120function f60<U>(x: T1<U>, y: T2<U>) { 121 x = y; 122 y = x; 123} 124 125type Identity<T> = { 126 [P in keyof T]: T[P]; 127} 128 129function f61<U>(x: Identity<U>, y: Partial<U>) { 130 x = y; // Error 131 y = x; 132} 133 134function f62<U>(x: Identity<U>, y: Readonly<U>) { 135 x = y; 136 y = x; 137} 138 139function f70<T>(x: { [P in keyof T]: T[P] }, y: { [P in keyof T]: T[P] }) { 140 x = y; 141 y = x; 142} 143 144function f71<T, U extends T>(x: { [P in keyof T]: T[P] }, y: { [P in keyof T]: U[P] }) { 145 x = y; 146 y = x; // Error 147} 148 149function f72<T, U extends T>(x: { [P in keyof T]: T[P] }, y: { [P in keyof U]: U[P] }) { 150 x = y; 151 y = x; // Error 152} 153 154function f73<T, K extends keyof T>(x: { [P in K]: T[P] }, y: { [P in keyof T]: T[P] }) { 155 x = y; 156 y = x; // Error 157} 158 159function f74<T, U extends T, K extends keyof T>(x: { [P in K]: T[P] }, y: { [P in keyof U]: U[P] }) { 160 x = y; 161 y = x; // Error 162} 163 164function f75<T, U extends T, K extends keyof T>(x: { [P in K]: T[P] }, y: { [P in keyof T]: U[P] }) { 165 x = y; 166 y = x; // Error 167} 168 169function f76<T, U extends T, K extends keyof T>(x: { [P in K]: T[P] }, y: { [P in K]: U[P] }) { 170 x = y; 171 y = x; // Error 172} 173 174function f80<T>(t: T): Partial<T> { 175 return t; 176} 177 178function f81<T, K extends keyof T>(t: T, k: K): Partial<T[K]> { 179 return t[k]; 180} 181 182function f82<T, K1 extends keyof T, K2 extends keyof T[K1]>(t: T, k1: K1, k2: K2): Partial<T[K1][K2]> { 183 return t[k1][k2]; 184} 185 186// #31070 187type Numeric<T> = { [K in keyof T]?: number }; 188function f90<T extends { x: number }>() { 189 const n: Numeric<T> = { x: 1 }; 190} 191 192function f<T extends { x: {} }>(): Partial<T> { 193 return undefined! as T; 194} 195