File: paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.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 (53 lines) | stat: -rw-r--r-- 3,027 bytes parent folder | download | duplicates (4)
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
tests/cases/compiler/paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts(27,23): error TS2345: Argument of type '{ x?: number[] | undefined; y?: string[] | undefined; }' is not assignable to parameter of type '{ y?: number[] | undefined; }'.
  Types of property 'y' are incompatible.
    Type 'string[] | undefined' is not assignable to type 'number[] | undefined'.
      Type 'string[]' is not assignable to type 'number[]'.
        Type 'string' is not assignable to type 'number'.
tests/cases/compiler/paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts(28,23): error TS2345: Argument of type '{ x?: number[] | undefined; y?: string[] | undefined; }' is not assignable to parameter of type '{ x?: string[] | undefined; }'.
  Types of property 'x' are incompatible.
    Type 'number[] | undefined' is not assignable to type 'string[] | undefined'.
      Type 'number[]' is not assignable to type 'string[]'.
        Type 'number' is not assignable to type 'string'.


==== tests/cases/compiler/paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts (2 errors) ====
    // Using a homomorphic mapped type over `T`
    // Produces a lower-priority inference for `T` than other
    // positions, allowing one to override the priority the argument
    // order would usually imply
    type Lower<T> = { [K in keyof T]: T[K] };
    
    export function appendToOptionalArray<
      K extends string | number | symbol,
      T
    >(
      object: { [x in K]?: Lower<T>[] },
      key: K,
      value: T
    ) {
      const array = object[key];
      if (array) {
        array.push(value);
      } else {
        object[key] = [value];
      }
    }
    
    // e.g.
    const foo: {x?: number[]; y?: string[]; } = {};
    appendToOptionalArray(foo, 'x', 123);   // ok
    appendToOptionalArray(foo, 'y', 'bar'); // ok
    appendToOptionalArray(foo, 'y', 12);    // should fail
                          ~~~
!!! error TS2345: Argument of type '{ x?: number[] | undefined; y?: string[] | undefined; }' is not assignable to parameter of type '{ y?: number[] | undefined; }'.
!!! error TS2345:   Types of property 'y' are incompatible.
!!! error TS2345:     Type 'string[] | undefined' is not assignable to type 'number[] | undefined'.
!!! error TS2345:       Type 'string[]' is not assignable to type 'number[]'.
!!! error TS2345:         Type 'string' is not assignable to type 'number'.
    appendToOptionalArray(foo, 'x', "no");  // should fail
                          ~~~
!!! error TS2345: Argument of type '{ x?: number[] | undefined; y?: string[] | undefined; }' is not assignable to parameter of type '{ x?: string[] | undefined; }'.
!!! error TS2345:   Types of property 'x' are incompatible.
!!! error TS2345:     Type 'number[] | undefined' is not assignable to type 'string[] | undefined'.
!!! error TS2345:       Type 'number[]' is not assignable to type 'string[]'.
!!! error TS2345:         Type 'number' is not assignable to type 'string'.