File: unionTypeFromArrayLiteral.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 (31 lines) | stat: -rw-r--r-- 1,754 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
tests/cases/conformance/types/union/unionTypeFromArrayLiteral.ts(9,5): error TS2322: Type '[number, string, string]' is not assignable to type '[number, string]'.
  Source has 3 element(s) but target allows only 2.


==== tests/cases/conformance/types/union/unionTypeFromArrayLiteral.ts (1 errors) ====
    // The resulting type an array literal expression is determined as follows:
    // If the array literal is empty, the resulting type is an array type with the element type Undefined.
    // Otherwise, if the array literal is contextually typed by a type that has a property with the numeric name ‘0’, the resulting type is a tuple type constructed from the types of the element expressions.
    // Otherwise, the resulting type is an array type with an element type that is the union of the types of the element expressions.
    
    var arr1 = [1, 2]; // number[]
    var arr2 = ["hello", true]; // (string | number)[]
    var arr3Tuple: [number, string] = [3, "three"]; // [number, string]
    var arr4Tuple: [number, string] = [3, "three", "hello"]; // [number, string, string]
        ~~~~~~~~~
!!! error TS2322: Type '[number, string, string]' is not assignable to type '[number, string]'.
!!! error TS2322:   Source has 3 element(s) but target allows only 2.
    var arrEmpty = [];
    var arr5Tuple: {
        0: string;
        5: number;
    } = ["hello", true, false, " hello", true, 10, "any"]; // Tuple
    class C { foo() { } }
    class D { foo2() { } }
    class E extends C { foo3() { } }
    class F extends C { foo4() { } }
    var c: C, d: D, e: E, f: F;
    var arr6 = [c, d];  // (C | D)[]
    var arr7 = [c, d, e]; // (C | D)[]
    var arr8 = [c, e]; // C[]
    var arr9 = [e, f]; // (E|F)[]