File: arrayLiterals3.errors.txt

package info (click to toggle)
node-typescript 5.0.4%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 459,116 kB
  • sloc: javascript: 1,972,754; makefile: 6; sh: 1
file content (69 lines) | stat: -rw-r--r-- 4,823 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
69
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(10,5): error TS2322: Type '[]' is not assignable to type '[any, any, any]'.
  Source has 0 element(s) but target requires 3.
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(11,38): error TS2322: Type 'string' is not assignable to type 'boolean'.
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(11,48): error TS2322: Type 'number' is not assignable to type 'string'.
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(11,51): error TS2322: Type 'boolean' is not assignable to type 'number'.
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(17,5): error TS2322: Type '[number, number, string, boolean]' is not assignable to type '[number, number]'.
  Source has 4 element(s) but target allows only 2.
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(33,5): error TS2322: Type 'number[]' is not assignable to type '[number, number, number]'.
  Target requires 3 element(s) but source may have fewer.
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(34,5): error TS2322: Type '(string | number)[]' is not assignable to type 'myArray'.
  Type 'string | number' is not assignable to type 'Number'.
    Type 'string' is not assignable to type 'Number'.


==== tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts (7 errors) ====
    // Each element expression in a non-empty array literal is processed as follows:
    //    - If the array literal contains no spread elements, and if the array literal is contextually typed (section 4.19)
    //      by a type T and T has a property with the numeric name N, where N is the index of the element expression in the array literal,
    //      the element expression is contextually typed by the type of that property.
    
    // The resulting type an array literal expression is determined as follows:
    //     - If the array literal contains no spread elements and is contextually typed by a tuple-like type,
    //       the resulting type is a tuple type constructed from the types of the element expressions.
    
    var a0: [any, any, any] = [];                             // Error
        ~~
!!! error TS2322: Type '[]' is not assignable to type '[any, any, any]'.
!!! error TS2322:   Source has 0 element(s) but target requires 3.
    var a1: [boolean, string, number] = ["string", 1, true];  // Error
                                         ~~~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'boolean'.
                                                   ~
!!! error TS2322: Type 'number' is not assignable to type 'string'.
                                                      ~~~~
!!! error TS2322: Type 'boolean' is not assignable to type 'number'.
    
    // The resulting type an array literal expression is determined as follows:
    //     - If the array literal contains no spread elements and is an array assignment pattern in a destructuring assignment (section 4.17.1),
    //       the resulting type is a tuple type constructed from the types of the element expressions.
    
    var [b1, b2]: [number, number] = [1, 2, "string", true];
        ~~~~~~~~
!!! error TS2322: Type '[number, number, string, boolean]' is not assignable to type '[number, number]'.
!!! error TS2322:   Source has 4 element(s) but target allows only 2.
    
    // The resulting type an array literal expression is determined as follows:
    //      - the resulting type is an array type with an element type that is the union of the types of the
    //        non - spread element expressions and the numeric index signature types of the spread element expressions
    var temp = ["s", "t", "r"];
    var temp1 = [1, 2, 3];
    var temp2: [number[], string[]] = [[1, 2, 3], ["hello", "string"]];
    
    interface tup {
        0: number[]|string[];
        1: number[]|string[];
    }
    interface myArray extends Array<Number> { }
    interface myArray2 extends Array<Number|String> { }
    var c0: tup = [...temp2];                         // Error
    var c1: [number, number, number] = [...temp1];    // Error cannot assign number[] to [number, number, number]
        ~~
!!! error TS2322: Type 'number[]' is not assignable to type '[number, number, number]'.
!!! error TS2322:   Target requires 3 element(s) but source may have fewer.
    var c2: myArray = [...temp1, ...temp];            // Error cannot assign (number|string)[] to number[]
        ~~
!!! error TS2322: Type '(string | number)[]' is not assignable to type 'myArray'.
!!! error TS2322:   Type 'string | number' is not assignable to type 'Number'.
!!! error TS2322:     Type 'string' is not assignable to type 'Number'.