File: mappedTypeConstraints2.errors.txt

package info (click to toggle)
node-typescript 5.0.4%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 459,140 kB
  • sloc: javascript: 1,972,754; makefile: 6; sh: 1
file content (68 lines) | stat: -rw-r--r-- 3,178 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
tests/cases/conformance/types/mapped/mappedTypeConstraints2.ts(10,11): error TS2322: Type 'Mapped2<K>[`get${K}`]' is not assignable to type '{ a: K; }'.
  Type 'Mapped2<K>[`get${string}`]' is not assignable to type '{ a: K; }'.
tests/cases/conformance/types/mapped/mappedTypeConstraints2.ts(16,11): error TS2322: Type 'Mapped3<K>[Uppercase<K>]' is not assignable to type '{ a: K; }'.
  Type 'Mapped3<K>[Uppercase<string>]' is not assignable to type '{ a: K; }'.
    Type 'Mapped3<K>[string]' is not assignable to type '{ a: K; }'.
tests/cases/conformance/types/mapped/mappedTypeConstraints2.ts(25,57): error TS2322: Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'.
  'T' could be instantiated with an arbitrary type which could be unrelated to 'Foo<T>[`get${T}`]'.


==== tests/cases/conformance/types/mapped/mappedTypeConstraints2.ts (3 errors) ====
    type Mapped1<K extends string> = { [P in K]: { a: P } };
    
    function f1<K extends string>(obj: Mapped1<K>, key: K) {
        const x: { a: K } = obj[key];
    }
    
    type Mapped2<K extends string> = { [P in K as `get${P}`]: { a: P } };
    
    function f2<K extends string>(obj: Mapped2<K>, key: `get${K}`) {
        const x: { a: K } = obj[key];  // Error
              ~
!!! error TS2322: Type 'Mapped2<K>[`get${K}`]' is not assignable to type '{ a: K; }'.
!!! error TS2322:   Type 'Mapped2<K>[`get${string}`]' is not assignable to type '{ a: K; }'.
    }
    
    type Mapped3<K extends string> = { [P in K as Uppercase<P>]: { a: P } };
    
    function f3<K extends string>(obj: Mapped3<K>, key: Uppercase<K>) {
        const x: { a: K } = obj[key];  // Error
              ~
!!! error TS2322: Type 'Mapped3<K>[Uppercase<K>]' is not assignable to type '{ a: K; }'.
!!! error TS2322:   Type 'Mapped3<K>[Uppercase<string>]' is not assignable to type '{ a: K; }'.
!!! error TS2322:     Type 'Mapped3<K>[string]' is not assignable to type '{ a: K; }'.
    }
    
    // Repro from #47794
    
    type Foo<T extends string> = {
        [RemappedT in T as `get${RemappedT}`]: RemappedT;
    };
    
    const get = <T extends string>(t: T, foo: Foo<T>): T => foo[`get${t}`];  // Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'
                                                            ~~~~~~~~~~~~~~
!!! error TS2322: Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'.
!!! error TS2322:   'T' could be instantiated with an arbitrary type which could be unrelated to 'Foo<T>[`get${T}`]'.
    
    // Repro from #48626
    
    interface Bounds {
        min: number;
        max: number;
    }
    
    type NumericBoundsOf<T> = {
        [K in keyof T as T[K] extends number | undefined ? K : never]: Bounds;
    }
    
    function validate<T extends object>(obj: T, bounds: NumericBoundsOf<T>) {
        for (const [key, val] of Object.entries(obj)) {
            const boundsForKey = bounds[key as keyof NumericBoundsOf<T>];
            if (boundsForKey) {
                const { min, max } = boundsForKey;
                if (min > val || max < val) return false;
            }
        }
        return true;
    }