Skip to content

Commit a931313

Browse files
committed
Add bigint tests
1 parent 95c24c6 commit a931313

30 files changed

+4371
-10
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
tests/cases/compiler/bigintIndex.ts(2,6): error TS1023: An index signature parameter type must be 'string' or 'number'.
2+
tests/cases/compiler/bigintIndex.ts(8,11): error TS2538: Type '1n' cannot be used as an index type.
3+
tests/cases/compiler/bigintIndex.ts(14,1): error TS2322: Type '123n' is not assignable to type 'string | number | symbol'.
4+
tests/cases/compiler/bigintIndex.ts(19,12): error TS2538: Type 'bigint' cannot be used as an index type.
5+
6+
7+
==== tests/cases/compiler/bigintIndex.ts (4 errors) ====
8+
interface BigIntIndex<E> {
9+
[index: bigint]: E; // should error
10+
~~~~~
11+
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
12+
}
13+
14+
const arr: number[] = [1, 2, 3];
15+
let num: number = arr[1];
16+
num = arr["1"];
17+
num = arr[1n]; // should error
18+
~~
19+
!!! error TS2538: Type '1n' cannot be used as an index type.
20+
21+
let key: keyof any; // should be type "string | number | symbol"
22+
key = 123;
23+
key = "abc";
24+
key = Symbol();
25+
key = 123n; // should error
26+
~~~
27+
!!! error TS2322: Type '123n' is not assignable to type 'string | number | symbol'.
28+
29+
// Show correct usage of bigint index: explicitly convert to string
30+
const bigNum: bigint = 0n;
31+
const typedArray = new Uint8Array(3);
32+
typedArray[bigNum] = 0xAA; // should error
33+
~~~~~~
34+
!!! error TS2538: Type 'bigint' cannot be used as an index type.
35+
typedArray[String(bigNum)] = 0xAA;
36+
typedArray["1"] = 0xBB;
37+
typedArray[2] = 0xCC;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//// [bigintIndex.ts]
2+
interface BigIntIndex<E> {
3+
[index: bigint]: E; // should error
4+
}
5+
6+
const arr: number[] = [1, 2, 3];
7+
let num: number = arr[1];
8+
num = arr["1"];
9+
num = arr[1n]; // should error
10+
11+
let key: keyof any; // should be type "string | number | symbol"
12+
key = 123;
13+
key = "abc";
14+
key = Symbol();
15+
key = 123n; // should error
16+
17+
// Show correct usage of bigint index: explicitly convert to string
18+
const bigNum: bigint = 0n;
19+
const typedArray = new Uint8Array(3);
20+
typedArray[bigNum] = 0xAA; // should error
21+
typedArray[String(bigNum)] = 0xAA;
22+
typedArray["1"] = 0xBB;
23+
typedArray[2] = 0xCC;
24+
25+
//// [bigintIndex.js]
26+
const arr = [1, 2, 3];
27+
let num = arr[1];
28+
num = arr["1"];
29+
num = arr[1n]; // should error
30+
let key; // should be type "string | number | symbol"
31+
key = 123;
32+
key = "abc";
33+
key = Symbol();
34+
key = 123n; // should error
35+
// Show correct usage of bigint index: explicitly convert to string
36+
const bigNum = 0n;
37+
const typedArray = new Uint8Array(3);
38+
typedArray[bigNum] = 0xAA; // should error
39+
typedArray[String(bigNum)] = 0xAA;
40+
typedArray["1"] = 0xBB;
41+
typedArray[2] = 0xCC;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
=== tests/cases/compiler/bigintIndex.ts ===
2+
interface BigIntIndex<E> {
3+
>BigIntIndex : Symbol(BigIntIndex, Decl(bigintIndex.ts, 0, 0))
4+
>E : Symbol(E, Decl(bigintIndex.ts, 0, 22))
5+
6+
[index: bigint]: E; // should error
7+
>index : Symbol(index, Decl(bigintIndex.ts, 1, 5))
8+
>E : Symbol(E, Decl(bigintIndex.ts, 0, 22))
9+
}
10+
11+
const arr: number[] = [1, 2, 3];
12+
>arr : Symbol(arr, Decl(bigintIndex.ts, 4, 5))
13+
14+
let num: number = arr[1];
15+
>num : Symbol(num, Decl(bigintIndex.ts, 5, 3))
16+
>arr : Symbol(arr, Decl(bigintIndex.ts, 4, 5))
17+
18+
num = arr["1"];
19+
>num : Symbol(num, Decl(bigintIndex.ts, 5, 3))
20+
>arr : Symbol(arr, Decl(bigintIndex.ts, 4, 5))
21+
22+
num = arr[1n]; // should error
23+
>num : Symbol(num, Decl(bigintIndex.ts, 5, 3))
24+
>arr : Symbol(arr, Decl(bigintIndex.ts, 4, 5))
25+
26+
let key: keyof any; // should be type "string | number | symbol"
27+
>key : Symbol(key, Decl(bigintIndex.ts, 9, 3))
28+
29+
key = 123;
30+
>key : Symbol(key, Decl(bigintIndex.ts, 9, 3))
31+
32+
key = "abc";
33+
>key : Symbol(key, Decl(bigintIndex.ts, 9, 3))
34+
35+
key = Symbol();
36+
>key : Symbol(key, Decl(bigintIndex.ts, 9, 3))
37+
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
38+
39+
key = 123n; // should error
40+
>key : Symbol(key, Decl(bigintIndex.ts, 9, 3))
41+
42+
// Show correct usage of bigint index: explicitly convert to string
43+
const bigNum: bigint = 0n;
44+
>bigNum : Symbol(bigNum, Decl(bigintIndex.ts, 16, 5))
45+
46+
const typedArray = new Uint8Array(3);
47+
>typedArray : Symbol(typedArray, Decl(bigintIndex.ts, 17, 5))
48+
>Uint8Array : Symbol(Uint8Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
49+
50+
typedArray[bigNum] = 0xAA; // should error
51+
>typedArray : Symbol(typedArray, Decl(bigintIndex.ts, 17, 5))
52+
>bigNum : Symbol(bigNum, Decl(bigintIndex.ts, 16, 5))
53+
54+
typedArray[String(bigNum)] = 0xAA;
55+
>typedArray : Symbol(typedArray, Decl(bigintIndex.ts, 17, 5))
56+
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --) ... and 1 more)
57+
>bigNum : Symbol(bigNum, Decl(bigintIndex.ts, 16, 5))
58+
59+
typedArray["1"] = 0xBB;
60+
>typedArray : Symbol(typedArray, Decl(bigintIndex.ts, 17, 5))
61+
62+
typedArray[2] = 0xCC;
63+
>typedArray : Symbol(typedArray, Decl(bigintIndex.ts, 17, 5))
64+
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
=== tests/cases/compiler/bigintIndex.ts ===
2+
interface BigIntIndex<E> {
3+
[index: bigint]: E; // should error
4+
>index : bigint
5+
}
6+
7+
const arr: number[] = [1, 2, 3];
8+
>arr : number[]
9+
>[1, 2, 3] : number[]
10+
>1 : 1
11+
>2 : 2
12+
>3 : 3
13+
14+
let num: number = arr[1];
15+
>num : number
16+
>arr[1] : number
17+
>arr : number[]
18+
>1 : 1
19+
20+
num = arr["1"];
21+
>num = arr["1"] : any
22+
>num : number
23+
>arr["1"] : any
24+
>arr : number[]
25+
>"1" : "1"
26+
27+
num = arr[1n]; // should error
28+
>num = arr[1n] : any
29+
>num : number
30+
>arr[1n] : any
31+
>arr : number[]
32+
>1n : 1n
33+
34+
let key: keyof any; // should be type "string | number | symbol"
35+
>key : string | number | symbol
36+
37+
key = 123;
38+
>key = 123 : 123
39+
>key : string | number | symbol
40+
>123 : 123
41+
42+
key = "abc";
43+
>key = "abc" : "abc"
44+
>key : string | number | symbol
45+
>"abc" : "abc"
46+
47+
key = Symbol();
48+
>key = Symbol() : symbol
49+
>key : string | number | symbol
50+
>Symbol() : symbol
51+
>Symbol : SymbolConstructor
52+
53+
key = 123n; // should error
54+
>key = 123n : 123n
55+
>key : string | number | symbol
56+
>123n : 123n
57+
58+
// Show correct usage of bigint index: explicitly convert to string
59+
const bigNum: bigint = 0n;
60+
>bigNum : bigint
61+
>0n : 0n
62+
63+
const typedArray = new Uint8Array(3);
64+
>typedArray : Uint8Array
65+
>new Uint8Array(3) : Uint8Array
66+
>Uint8Array : Uint8ArrayConstructor
67+
>3 : 3
68+
69+
typedArray[bigNum] = 0xAA; // should error
70+
>typedArray[bigNum] = 0xAA : 170
71+
>typedArray[bigNum] : any
72+
>typedArray : Uint8Array
73+
>bigNum : bigint
74+
>0xAA : 170
75+
76+
typedArray[String(bigNum)] = 0xAA;
77+
>typedArray[String(bigNum)] = 0xAA : 170
78+
>typedArray[String(bigNum)] : any
79+
>typedArray : Uint8Array
80+
>String(bigNum) : string
81+
>String : StringConstructor
82+
>bigNum : bigint
83+
>0xAA : 170
84+
85+
typedArray["1"] = 0xBB;
86+
>typedArray["1"] = 0xBB : 187
87+
>typedArray["1"] : any
88+
>typedArray : Uint8Array
89+
>"1" : "1"
90+
>0xBB : 187
91+
92+
typedArray[2] = 0xCC;
93+
>typedArray[2] = 0xCC : 204
94+
>typedArray[2] : number
95+
>typedArray : Uint8Array
96+
>2 : 2
97+
>0xCC : 204
98+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
tests/cases/compiler/bigintWithLib.ts(4,1): error TS2350: Only a void function can be called with the 'new' keyword.
2+
tests/cases/compiler/bigintWithLib.ts(16,33): error TS2345: Argument of type 'number[]' is not assignable to parameter of type 'ArrayBuffer | SharedArrayBuffer'.
3+
Type 'number[]' is not assignable to type 'SharedArrayBuffer'.
4+
Property 'byteLength' is missing in type 'number[]'.
5+
tests/cases/compiler/bigintWithLib.ts(21,13): error TS2540: Cannot assign to 'length' because it is a constant or a read-only property.
6+
tests/cases/compiler/bigintWithLib.ts(28,35): error TS2345: Argument of type 'number[]' is not assignable to parameter of type 'ArrayBuffer | SharedArrayBuffer'.
7+
Type 'number[]' is not assignable to type 'SharedArrayBuffer'.
8+
tests/cases/compiler/bigintWithLib.ts(33,13): error TS2540: Cannot assign to 'length' because it is a constant or a read-only property.
9+
tests/cases/compiler/bigintWithLib.ts(40,25): error TS2345: Argument of type '-1' is not assignable to parameter of type 'bigint'.
10+
tests/cases/compiler/bigintWithLib.ts(43,26): error TS2345: Argument of type '123' is not assignable to parameter of type 'bigint'.
11+
12+
13+
==== tests/cases/compiler/bigintWithLib.ts (7 errors) ====
14+
// Test BigInt functions
15+
let bigintVal: bigint = BigInt(123);
16+
bigintVal = BigInt("456");
17+
new BigInt(123); // should error
18+
~~~~~~~~~~~~~~~
19+
!!! error TS2350: Only a void function can be called with the 'new' keyword.
20+
bigintVal = BigInt.asIntN(8, 0xFFFFn);
21+
bigintVal = BigInt.asUintN(8, 0xFFFFn);
22+
bigintVal = bigintVal.valueOf();
23+
let stringVal: string = bigintVal.toString();
24+
stringVal = bigintVal.toString(2);
25+
stringVal = bigintVal.toLocaleString();
26+
27+
// Test BigInt64Array
28+
let bigIntArray: BigInt64Array = new BigInt64Array();
29+
bigIntArray = new BigInt64Array(10);
30+
bigIntArray = new BigInt64Array([1n, 2n, 3n]);
31+
bigIntArray = new BigInt64Array([1, 2, 3]); // should error
32+
~~~~~~~~~
33+
!!! error TS2345: Argument of type 'number[]' is not assignable to parameter of type 'ArrayBuffer | SharedArrayBuffer'.
34+
!!! error TS2345: Type 'number[]' is not assignable to type 'SharedArrayBuffer'.
35+
!!! error TS2345: Property 'byteLength' is missing in type 'number[]'.
36+
bigIntArray = new BigInt64Array(new ArrayBuffer(80));
37+
bigIntArray = new BigInt64Array(new ArrayBuffer(80), 8);
38+
bigIntArray = new BigInt64Array(new ArrayBuffer(80), 8, 3);
39+
let len: number = bigIntArray.length;
40+
bigIntArray.length = 10; // should error
41+
~~~~~~
42+
!!! error TS2540: Cannot assign to 'length' because it is a constant or a read-only property.
43+
let arrayBufferLike: ArrayBufferView = bigIntArray;
44+
45+
// Test BigUint64Array
46+
let bigUintArray: BigUint64Array = new BigUint64Array();
47+
bigUintArray = new BigUint64Array(10);
48+
bigUintArray = new BigUint64Array([1n, 2n, 3n]);
49+
bigUintArray = new BigUint64Array([1, 2, 3]); // should error
50+
~~~~~~~~~
51+
!!! error TS2345: Argument of type 'number[]' is not assignable to parameter of type 'ArrayBuffer | SharedArrayBuffer'.
52+
!!! error TS2345: Type 'number[]' is not assignable to type 'SharedArrayBuffer'.
53+
bigUintArray = new BigUint64Array(new ArrayBuffer(80));
54+
bigUintArray = new BigUint64Array(new ArrayBuffer(80), 8);
55+
bigUintArray = new BigUint64Array(new ArrayBuffer(80), 8, 3);
56+
len = bigIntArray.length;
57+
bigIntArray.length = 10; // should error
58+
~~~~~~
59+
!!! error TS2540: Cannot assign to 'length' because it is a constant or a read-only property.
60+
arrayBufferLike = bigIntArray;
61+
62+
// Test added DataView methods
63+
const dataView = new DataView(new ArrayBuffer(80));
64+
dataView.setBigInt64(1, -1n);
65+
dataView.setBigInt64(1, -1n, true);
66+
dataView.setBigInt64(1, -1); // should error
67+
~~
68+
!!! error TS2345: Argument of type '-1' is not assignable to parameter of type 'bigint'.
69+
dataView.setBigUint64(2, 123n);
70+
dataView.setBigUint64(2, 123n, true);
71+
dataView.setBigUint64(2, 123); // should error
72+
~~~
73+
!!! error TS2345: Argument of type '123' is not assignable to parameter of type 'bigint'.
74+
bigintVal = dataView.getBigInt64(1);
75+
bigintVal = dataView.getBigInt64(1, true);
76+
bigintVal = dataView.getBigUint64(2);
77+
bigintVal = dataView.getBigUint64(2, true);

0 commit comments

Comments
 (0)