|
| 1 | +//// [indexSignatureAndMappedType.ts] |
| 2 | +// A mapped type { [P in K]: X }, where K is a generic type, is related to |
| 3 | +// { [key: string]: Y } if X is related to Y. |
| 4 | + |
| 5 | +function f1<T, K extends string>(x: { [key: string]: T }, y: Record<K, T>) { |
| 6 | + x = y; |
| 7 | + y = x; // Error |
| 8 | +} |
| 9 | + |
| 10 | +function f2<T, K extends string>(x: { [key: string]: T }, y: Record<string, T>) { |
| 11 | + x = y; |
| 12 | + y = x; |
| 13 | +} |
| 14 | + |
| 15 | +function f3<T, U, K extends string>(x: { [key: string]: T }, y: Record<K, U>) { |
| 16 | + x = y; // Error |
| 17 | + y = x; // Error |
| 18 | +} |
| 19 | + |
| 20 | +// Repro from #14548 |
| 21 | + |
| 22 | +type Dictionary = { |
| 23 | + [key: string]: string; |
| 24 | +}; |
| 25 | + |
| 26 | +interface IBaseEntity { |
| 27 | + name: string; |
| 28 | + properties: Dictionary; |
| 29 | +} |
| 30 | + |
| 31 | +interface IEntity<T extends string> extends IBaseEntity { |
| 32 | + properties: Record<T, string>; |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +//// [indexSignatureAndMappedType.js] |
| 37 | +"use strict"; |
| 38 | +// A mapped type { [P in K]: X }, where K is a generic type, is related to |
| 39 | +// { [key: string]: Y } if X is related to Y. |
| 40 | +function f1(x, y) { |
| 41 | + x = y; |
| 42 | + y = x; // Error |
| 43 | +} |
| 44 | +function f2(x, y) { |
| 45 | + x = y; |
| 46 | + y = x; |
| 47 | +} |
| 48 | +function f3(x, y) { |
| 49 | + x = y; // Error |
| 50 | + y = x; // Error |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +//// [indexSignatureAndMappedType.d.ts] |
| 55 | +declare function f1<T, K extends string>(x: { |
| 56 | + [key: string]: T; |
| 57 | +}, y: Record<K, T>): void; |
| 58 | +declare function f2<T, K extends string>(x: { |
| 59 | + [key: string]: T; |
| 60 | +}, y: Record<string, T>): void; |
| 61 | +declare function f3<T, U, K extends string>(x: { |
| 62 | + [key: string]: T; |
| 63 | +}, y: Record<K, U>): void; |
| 64 | +declare type Dictionary = { |
| 65 | + [key: string]: string; |
| 66 | +}; |
| 67 | +interface IBaseEntity { |
| 68 | + name: string; |
| 69 | + properties: Dictionary; |
| 70 | +} |
| 71 | +interface IEntity<T extends string> extends IBaseEntity { |
| 72 | + properties: Record<T, string>; |
| 73 | +} |
0 commit comments