File: indexSignatureAndMappedType.errors.txt

package info (click to toggle)
node-typescript 4.9.5%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 533,908 kB
  • sloc: javascript: 2,018,330; makefile: 7; sh: 1
file content (50 lines) | stat: -rw-r--r-- 2,101 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
tests/cases/compiler/indexSignatureAndMappedType.ts(6,5): error TS2322: Type '{ [key: string]: T; }' is not assignable to type 'Record<K, T>'.
tests/cases/compiler/indexSignatureAndMappedType.ts(15,5): error TS2322: Type 'Record<K, U>' is not assignable to type '{ [key: string]: T; }'.
  Type 'U' is not assignable to type 'T'.
    'T' could be instantiated with an arbitrary type which could be unrelated to 'U'.
tests/cases/compiler/indexSignatureAndMappedType.ts(16,5): error TS2322: Type '{ [key: string]: T; }' is not assignable to type 'Record<K, U>'.


==== tests/cases/compiler/indexSignatureAndMappedType.ts (3 errors) ====
    // A mapped type { [P in K]: X }, where K is a generic type, is related to
    // { [key: string]: Y } if X is related to Y.
    
    function f1<T, K extends string>(x: { [key: string]: T }, y: Record<K, T>) {
        x = y;
        y = x;  // Error
        ~
!!! error TS2322: Type '{ [key: string]: T; }' is not assignable to type 'Record<K, T>'.
    }
    
    function f2<T>(x: { [key: string]: T }, y: Record<string, T>) {
        x = y;
        y = x;
    }
    
    function f3<T, U, K extends string>(x: { [key: string]: T }, y: Record<K, U>) {
        x = y;  // Error
        ~
!!! error TS2322: Type 'Record<K, U>' is not assignable to type '{ [key: string]: T; }'.
!!! error TS2322:   Type 'U' is not assignable to type 'T'.
!!! error TS2322:     'T' could be instantiated with an arbitrary type which could be unrelated to 'U'.
!!! related TS2208 tests/cases/compiler/indexSignatureAndMappedType.ts:14:16: This type parameter might need an `extends T` constraint.
        y = x;  // Error
        ~
!!! error TS2322: Type '{ [key: string]: T; }' is not assignable to type 'Record<K, U>'.
    }
    
    // Repro from #14548
    
    type Dictionary = {
        [key: string]: string;
    };
    
    interface IBaseEntity {
        name: string;
        properties: Dictionary;
    }
    
    interface IEntity<T extends string> extends IBaseEntity {
        properties: Record<T, string>;
    }