File: bigintIndex.types

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 (121 lines) | stat: -rw-r--r-- 2,398 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
=== tests/cases/compiler/a.ts ===
interface BigIntIndex<E> {
    [index: bigint]: E; // should error
>index : bigint
}

const arr: number[] = [1, 2, 3];
>arr : number[]
>[1, 2, 3] : number[]
>1 : 1
>2 : 2
>3 : 3

let num: number = arr[1];
>num : number
>arr[1] : number
>arr : number[]
>1 : 1

num = arr["1"];
>num = arr["1"] : number
>num : number
>arr["1"] : number
>arr : number[]
>"1" : "1"

num = arr[1n]; // should error
>num = arr[1n] : any
>num : number
>arr[1n] : any
>arr : number[]
>1n : 1n

let key: keyof any; // should be type "string | number | symbol"
>key : string | number | symbol

key = 123;
>key = 123 : 123
>key : string | number | symbol
>123 : 123

key = "abc";
>key = "abc" : "abc"
>key : string | number | symbol
>"abc" : "abc"

key = Symbol();
>key = Symbol() : symbol
>key : string | number | symbol
>Symbol() : symbol
>Symbol : SymbolConstructor

key = 123n; // should error
>key = 123n : 123n
>key : string | number | symbol
>123n : 123n

// Show correct usage of bigint index: explicitly convert to string
const bigNum: bigint = 0n;
>bigNum : bigint
>0n : 0n

const typedArray = new Uint8Array(3);
>typedArray : Uint8Array
>new Uint8Array(3) : Uint8Array
>Uint8Array : Uint8ArrayConstructor
>3 : 3

typedArray[bigNum] = 0xAA; // should error
>typedArray[bigNum] = 0xAA : 170
>typedArray[bigNum] : any
>typedArray : Uint8Array
>bigNum : bigint
>0xAA : 170

typedArray[String(bigNum)] = 0xAA;
>typedArray[String(bigNum)] = 0xAA : 170
>typedArray[String(bigNum)] : any
>typedArray : Uint8Array
>String(bigNum) : string
>String : StringConstructor
>bigNum : bigint
>0xAA : 170

typedArray["1"] = 0xBB;
>typedArray["1"] = 0xBB : 187
>typedArray["1"] : number
>typedArray : Uint8Array
>"1" : "1"
>0xBB : 187

typedArray[2] = 0xCC;
>typedArray[2] = 0xCC : 204
>typedArray[2] : number
>typedArray : Uint8Array
>2 : 2
>0xCC : 204

// {1n: 123} is a syntax error; must go in separate file so BigIntIndex error is shown
=== tests/cases/compiler/b.ts ===
// BigInt cannot be used as an object literal property
const a = {1n: 123};
>a : {}
>{ : {}
>1n : 1n
>123 : 123

const b = {[1n]: 456};
>b : {}
>{[1n]: 456} : {}
>[1n] : number
>1n : 1n
>456 : 456

const c = {[bigNum]: 789};
>c : {}
>{[bigNum]: 789} : {}
>[bigNum] : number
>bigNum : bigint
>789 : 789