Skip to content

Commit 42baf32

Browse files
committed
Added tests for constant JSON imports
1 parent c5b1dee commit 42baf32

9 files changed

Lines changed: 509 additions & 0 deletions
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//// [tests/cases/compiler/jsonLiteralTypes.ts] ////
2+
3+
//// [data.json]
4+
{
5+
"s": "string literal",
6+
"n": 123,
7+
"b": true,
8+
"arr": ["a", "b"]
9+
}
10+
11+
//// [main.ts]
12+
import data from "./data.json";
13+
14+
const s: "string literal" = data.s;
15+
const n: 123 = data.n;
16+
const b: true = data.b;
17+
const arr: readonly ["a", "b"] = data.arr;
18+
19+
20+
//// [main.js]
21+
"use strict";
22+
var __importDefault = (this && this.__importDefault) || function (mod) {
23+
return (mod && mod.__esModule) ? mod : { "default": mod };
24+
};
25+
Object.defineProperty(exports, "__esModule", { value: true });
26+
const data_json_1 = __importDefault(require("./data.json"));
27+
const s = data_json_1.default.s;
28+
const n = data_json_1.default.n;
29+
const b = data_json_1.default.b;
30+
const arr = data_json_1.default.arr;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//// [tests/cases/compiler/jsonLiteralTypes.ts] ////
2+
3+
=== data.json ===
4+
{
5+
"s": "string literal",
6+
>"s" : Symbol("s", Decl(data.json, 0, 1))
7+
8+
"n": 123,
9+
>"n" : Symbol("n", Decl(data.json, 1, 26))
10+
11+
"b": true,
12+
>"b" : Symbol("b", Decl(data.json, 2, 13))
13+
14+
"arr": ["a", "b"]
15+
>"arr" : Symbol("arr", Decl(data.json, 3, 14))
16+
}
17+
18+
=== main.ts ===
19+
import data from "./data.json";
20+
>data : Symbol(data, Decl(main.ts, 0, 6))
21+
22+
const s: "string literal" = data.s;
23+
>s : Symbol(s, Decl(main.ts, 2, 5))
24+
>data.s : Symbol("s", Decl(data.json, 0, 1))
25+
>data : Symbol(data, Decl(main.ts, 0, 6))
26+
>s : Symbol("s", Decl(data.json, 0, 1))
27+
28+
const n: 123 = data.n;
29+
>n : Symbol(n, Decl(main.ts, 3, 5))
30+
>data.n : Symbol("n", Decl(data.json, 1, 26))
31+
>data : Symbol(data, Decl(main.ts, 0, 6))
32+
>n : Symbol("n", Decl(data.json, 1, 26))
33+
34+
const b: true = data.b;
35+
>b : Symbol(b, Decl(main.ts, 4, 5))
36+
>data.b : Symbol("b", Decl(data.json, 2, 13))
37+
>data : Symbol(data, Decl(main.ts, 0, 6))
38+
>b : Symbol("b", Decl(data.json, 2, 13))
39+
40+
const arr: readonly ["a", "b"] = data.arr;
41+
>arr : Symbol(arr, Decl(main.ts, 5, 5))
42+
>data.arr : Symbol("arr", Decl(data.json, 3, 14))
43+
>data : Symbol(data, Decl(main.ts, 0, 6))
44+
>arr : Symbol("arr", Decl(data.json, 3, 14))
45+
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//// [tests/cases/compiler/jsonLiteralTypes.ts] ////
2+
3+
=== data.json ===
4+
{
5+
>{ "s": "string literal", "n": 123, "b": true, "arr": ["a", "b"]} : { readonly s: "string literal"; readonly n: 123; readonly b: true; readonly arr: readonly ["a", "b"]; }
6+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7+
8+
"s": "string literal",
9+
>"s" : "string literal"
10+
> : ^^^^^^^^^^^^^^^^
11+
>"string literal" : "string literal"
12+
> : ^^^^^^^^^^^^^^^^
13+
14+
"n": 123,
15+
>"n" : 123
16+
> : ^^^
17+
>123 : 123
18+
> : ^^^
19+
20+
"b": true,
21+
>"b" : true
22+
> : ^^^^
23+
>true : true
24+
> : ^^^^
25+
26+
"arr": ["a", "b"]
27+
>"arr" : readonly ["a", "b"]
28+
> : ^^^^^^^^^^^^^^^^^^^
29+
>["a", "b"] : readonly ["a", "b"]
30+
> : ^^^^^^^^^^^^^^^^^^^
31+
>"a" : "a"
32+
> : ^^^
33+
>"b" : "b"
34+
> : ^^^
35+
}
36+
37+
=== main.ts ===
38+
import data from "./data.json";
39+
>data : { readonly s: "string literal"; readonly n: 123; readonly b: true; readonly arr: readonly ["a", "b"]; }
40+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
41+
42+
const s: "string literal" = data.s;
43+
>s : "string literal"
44+
> : ^^^^^^^^^^^^^^^^
45+
>data.s : "string literal"
46+
> : ^^^^^^^^^^^^^^^^
47+
>data : { readonly s: "string literal"; readonly n: 123; readonly b: true; readonly arr: readonly ["a", "b"]; }
48+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
49+
>s : "string literal"
50+
> : ^^^^^^^^^^^^^^^^
51+
52+
const n: 123 = data.n;
53+
>n : 123
54+
> : ^^^
55+
>data.n : 123
56+
> : ^^^
57+
>data : { readonly s: "string literal"; readonly n: 123; readonly b: true; readonly arr: readonly ["a", "b"]; }
58+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
59+
>n : 123
60+
> : ^^^
61+
62+
const b: true = data.b;
63+
>b : true
64+
> : ^^^^
65+
>true : true
66+
> : ^^^^
67+
>data.b : true
68+
> : ^^^^
69+
>data : { readonly s: "string literal"; readonly n: 123; readonly b: true; readonly arr: readonly ["a", "b"]; }
70+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
71+
>b : true
72+
> : ^^^^
73+
74+
const arr: readonly ["a", "b"] = data.arr;
75+
>arr : readonly ["a", "b"]
76+
> : ^^^^^^^^^^^^^^^^^^^
77+
>data.arr : readonly ["a", "b"]
78+
> : ^^^^^^^^^^^^^^^^^^^
79+
>data : { readonly s: "string literal"; readonly n: 123; readonly b: true; readonly arr: readonly ["a", "b"]; }
80+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
81+
>arr : readonly ["a", "b"]
82+
> : ^^^^^^^^^^^^^^^^^^^
83+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
main.ts(14,7): error TS2322: Type 'string' is not assignable to type '"string literal"'.
2+
main.ts(15,7): error TS2322: Type 'number' is not assignable to type '123'.
3+
main.ts(16,7): error TS2322: Type 'boolean' is not assignable to type 'true'.
4+
main.ts(17,7): error TS2322: Type 'string[]' is not assignable to type 'readonly ["a", "b"]'.
5+
Target requires 2 element(s) but source may have fewer.
6+
7+
8+
==== data.json (0 errors) ====
9+
{
10+
"s": "string literal",
11+
"n": 123,
12+
"b": true,
13+
"arr": ["a", "b"]
14+
}
15+
16+
==== main.ts (4 errors) ====
17+
import data from "./data.json";
18+
19+
// Should be wide types
20+
const s: string = data.s;
21+
const n: number = data.n;
22+
const b: boolean = data.b;
23+
const arr: string[] = data.arr;
24+
25+
// Should NOT be literal types (these assignments should fail if they were literals, but since we are assigning TO them, we check what they ARE)
26+
// Actually, data.s is string. So `const x: "literal" = data.s` would fail if data.s is string.
27+
// But here data.s IS string.
28+
// Let's verify by assigning to literals which should fail if it is string.
29+
30+
const literalS: "string literal" = data.s; // Error expected: string not assignable to "string literal"
31+
~~~~~~~~
32+
!!! error TS2322: Type 'string' is not assignable to type '"string literal"'.
33+
const literalN: 123 = data.n; // Error expected
34+
~~~~~~~~
35+
!!! error TS2322: Type 'number' is not assignable to type '123'.
36+
const literalB: true = data.b; // Error expected
37+
~~~~~~~~
38+
!!! error TS2322: Type 'boolean' is not assignable to type 'true'.
39+
const literalArr: readonly ["a", "b"] = data.arr; // Error expected
40+
~~~~~~~~~~
41+
!!! error TS2322: Type 'string[]' is not assignable to type 'readonly ["a", "b"]'.
42+
!!! error TS2322: Target requires 2 element(s) but source may have fewer.
43+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//// [tests/cases/compiler/jsonLiteralTypesDefault.ts] ////
2+
3+
//// [data.json]
4+
{
5+
"s": "string literal",
6+
"n": 123,
7+
"b": true,
8+
"arr": ["a", "b"]
9+
}
10+
11+
//// [main.ts]
12+
import data from "./data.json";
13+
14+
// Should be wide types
15+
const s: string = data.s;
16+
const n: number = data.n;
17+
const b: boolean = data.b;
18+
const arr: string[] = data.arr;
19+
20+
// Should NOT be literal types (these assignments should fail if they were literals, but since we are assigning TO them, we check what they ARE)
21+
// Actually, data.s is string. So `const x: "literal" = data.s` would fail if data.s is string.
22+
// But here data.s IS string.
23+
// Let's verify by assigning to literals which should fail if it is string.
24+
25+
const literalS: "string literal" = data.s; // Error expected: string not assignable to "string literal"
26+
const literalN: 123 = data.n; // Error expected
27+
const literalB: true = data.b; // Error expected
28+
const literalArr: readonly ["a", "b"] = data.arr; // Error expected
29+
30+
31+
//// [main.js]
32+
"use strict";
33+
var __importDefault = (this && this.__importDefault) || function (mod) {
34+
return (mod && mod.__esModule) ? mod : { "default": mod };
35+
};
36+
Object.defineProperty(exports, "__esModule", { value: true });
37+
const data_json_1 = __importDefault(require("./data.json"));
38+
// Should be wide types
39+
const s = data_json_1.default.s;
40+
const n = data_json_1.default.n;
41+
const b = data_json_1.default.b;
42+
const arr = data_json_1.default.arr;
43+
// Should NOT be literal types (these assignments should fail if they were literals, but since we are assigning TO them, we check what they ARE)
44+
// Actually, data.s is string. So `const x: "literal" = data.s` would fail if data.s is string.
45+
// But here data.s IS string.
46+
// Let's verify by assigning to literals which should fail if it is string.
47+
const literalS = data_json_1.default.s; // Error expected: string not assignable to "string literal"
48+
const literalN = data_json_1.default.n; // Error expected
49+
const literalB = data_json_1.default.b; // Error expected
50+
const literalArr = data_json_1.default.arr; // Error expected
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//// [tests/cases/compiler/jsonLiteralTypesDefault.ts] ////
2+
3+
=== data.json ===
4+
{
5+
"s": "string literal",
6+
>"s" : Symbol("s", Decl(data.json, 0, 1))
7+
8+
"n": 123,
9+
>"n" : Symbol("n", Decl(data.json, 1, 26))
10+
11+
"b": true,
12+
>"b" : Symbol("b", Decl(data.json, 2, 13))
13+
14+
"arr": ["a", "b"]
15+
>"arr" : Symbol("arr", Decl(data.json, 3, 14))
16+
}
17+
18+
=== main.ts ===
19+
import data from "./data.json";
20+
>data : Symbol(data, Decl(main.ts, 0, 6))
21+
22+
// Should be wide types
23+
const s: string = data.s;
24+
>s : Symbol(s, Decl(main.ts, 3, 5))
25+
>data.s : Symbol("s", Decl(data.json, 0, 1))
26+
>data : Symbol(data, Decl(main.ts, 0, 6))
27+
>s : Symbol("s", Decl(data.json, 0, 1))
28+
29+
const n: number = data.n;
30+
>n : Symbol(n, Decl(main.ts, 4, 5))
31+
>data.n : Symbol("n", Decl(data.json, 1, 26))
32+
>data : Symbol(data, Decl(main.ts, 0, 6))
33+
>n : Symbol("n", Decl(data.json, 1, 26))
34+
35+
const b: boolean = data.b;
36+
>b : Symbol(b, Decl(main.ts, 5, 5))
37+
>data.b : Symbol("b", Decl(data.json, 2, 13))
38+
>data : Symbol(data, Decl(main.ts, 0, 6))
39+
>b : Symbol("b", Decl(data.json, 2, 13))
40+
41+
const arr: string[] = data.arr;
42+
>arr : Symbol(arr, Decl(main.ts, 6, 5))
43+
>data.arr : Symbol("arr", Decl(data.json, 3, 14))
44+
>data : Symbol(data, Decl(main.ts, 0, 6))
45+
>arr : Symbol("arr", Decl(data.json, 3, 14))
46+
47+
// Should NOT be literal types (these assignments should fail if they were literals, but since we are assigning TO them, we check what they ARE)
48+
// Actually, data.s is string. So `const x: "literal" = data.s` would fail if data.s is string.
49+
// But here data.s IS string.
50+
// Let's verify by assigning to literals which should fail if it is string.
51+
52+
const literalS: "string literal" = data.s; // Error expected: string not assignable to "string literal"
53+
>literalS : Symbol(literalS, Decl(main.ts, 13, 5))
54+
>data.s : Symbol("s", Decl(data.json, 0, 1))
55+
>data : Symbol(data, Decl(main.ts, 0, 6))
56+
>s : Symbol("s", Decl(data.json, 0, 1))
57+
58+
const literalN: 123 = data.n; // Error expected
59+
>literalN : Symbol(literalN, Decl(main.ts, 14, 5))
60+
>data.n : Symbol("n", Decl(data.json, 1, 26))
61+
>data : Symbol(data, Decl(main.ts, 0, 6))
62+
>n : Symbol("n", Decl(data.json, 1, 26))
63+
64+
const literalB: true = data.b; // Error expected
65+
>literalB : Symbol(literalB, Decl(main.ts, 15, 5))
66+
>data.b : Symbol("b", Decl(data.json, 2, 13))
67+
>data : Symbol(data, Decl(main.ts, 0, 6))
68+
>b : Symbol("b", Decl(data.json, 2, 13))
69+
70+
const literalArr: readonly ["a", "b"] = data.arr; // Error expected
71+
>literalArr : Symbol(literalArr, Decl(main.ts, 16, 5))
72+
>data.arr : Symbol("arr", Decl(data.json, 3, 14))
73+
>data : Symbol(data, Decl(main.ts, 0, 6))
74+
>arr : Symbol("arr", Decl(data.json, 3, 14))
75+

0 commit comments

Comments
 (0)