Skip to content

Commit 9eb0c9a

Browse files
committed
Use widened type (just like importing using module.exports = in js file)
Fixes microsoft#26429
1 parent 50ccd91 commit 9eb0c9a

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5092,7 +5092,14 @@ namespace ts {
50925092
// Handle export default expressions
50935093
if (isSourceFile(declaration)) {
50945094
const jsonSourceFile = cast(declaration, isJsonSourceFile);
5095-
return jsonSourceFile.statements.length ? checkExpression(jsonSourceFile.statements[0].expression) : emptyObjectType;
5095+
if (!jsonSourceFile.statements.length) {
5096+
return emptyObjectType;
5097+
}
5098+
const type = getWidenedLiteralType(checkExpression(jsonSourceFile.statements[0].expression));
5099+
if (type.flags & TypeFlags.Object) {
5100+
return getRegularTypeOfObjectLiteral(type);
5101+
}
5102+
return type;
50965103
}
50975104
if (declaration.kind === SyntaxKind.ExportAssignment) {
50985105
return checkExpression((<ExportAssignment>declaration).expression);

tests/baselines/reference/requireOfJsonFileInJsFile.errors.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
/user.js(2,7): error TS2339: Property 'b' does not exist on type '{ "a": number; }'.
2+
/user.js(5,7): error TS2322: Type '{ "a": number; }' is not assignable to type '{ b: number; }'.
3+
Property 'b' is missing in type '{ "a": number; }'.
24
/user.js(9,7): error TS2339: Property 'b' does not exist on type '{ "a": number; }'.
35
/user.js(12,7): error TS2322: Type '{ a: number; }' is not assignable to type '{ b: number; }'.
46
Property 'b' is missing in type '{ a: number; }'.
57

68

7-
==== /user.js (3 errors) ====
9+
==== /user.js (4 errors) ====
810
const json0 = require("./json.json");
911
json0.b; // Error (good)
1012
~
1113
!!! error TS2339: Property 'b' does not exist on type '{ "a": number; }'.
1214

1315
/** @type {{ b: number }} */
1416
const json1 = require("./json.json"); // No error (bad)
17+
~~~~~
18+
!!! error TS2322: Type '{ "a": number; }' is not assignable to type '{ b: number; }'.
19+
!!! error TS2322: Property 'b' is missing in type '{ "a": number; }'.
1520
json1.b; // No error (OK since that's the type annotation)
1621

1722
const js0 = require("./js.js");

tests/baselines/reference/requireOfJsonFileTypes.types

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import c = require('./c.json');
66
>c : (string | null)[]
77

88
import d = require('./d.json');
9-
>d : "dConfig"
9+
>d : string
1010

1111
import e = require('./e.json');
12-
>e : -10
12+
>e : number
1313

1414
import f = require('./f.json');
1515
>f : number[]
@@ -64,14 +64,14 @@ const stringOrNumberOrNull: string | number | null = c[0];
6464
>0 : 0
6565

6666
stringLiteral = d;
67-
>stringLiteral = d : "dConfig"
67+
>stringLiteral = d : string
6868
>stringLiteral : string
69-
>d : "dConfig"
69+
>d : string
7070

7171
numberLiteral = e;
72-
>numberLiteral = e : -10
72+
>numberLiteral = e : number
7373
>numberLiteral : number
74-
>e : -10
74+
>e : number
7575

7676
numberLiteral = f[0];
7777
>numberLiteral = f[0] : number

0 commit comments

Comments
 (0)