TypeScript/tests/baselines/reference/requireOfJsonFileInJsFile.types
Nathan Shively-Sanders aba932aefa
Create synthetic exports symbol for commonjs module (#42655)
* Commonjs module:create synthetic exports symbol

Previously, the `module` identifier in commonjs modules got a synthetic
type with a single property `exports`. The exports property reused the
file's symbol, which, for a module file, gives the correct exported
properties.

However, the name of this symbol was still the filename of the file, not
`exports`. This PR creates a synthetic symbol for `exports` by copying
in a similar way to esModuleInterop's `default` symbol in
`resolveESModuleSymbol` (although the intent there is to strip off
signatures from the symbol).

* correct parent of synthetic symbol
2021-02-05 10:56:03 -08:00

64 lines
1.3 KiB
Plaintext

=== /user.js ===
const json0 = require("./json.json");
>json0 : { a: number; }
>require("./json.json") : { a: number; }
>require : any
>"./json.json" : "./json.json"
json0.b; // Error (good)
>json0.b : any
>json0 : { a: number; }
>b : any
/** @type {{ b: number }} */
const json1 = require("./json.json"); // No error (bad)
>json1 : { b: number; }
>require("./json.json") : { a: number; }
>require : any
>"./json.json" : "./json.json"
json1.b; // No error (OK since that's the type annotation)
>json1.b : number
>json1 : { b: number; }
>b : number
const js0 = require("./js.js");
>js0 : { a: number; }
>require("./js.js") : { a: number; }
>require : any
>"./js.js" : "./js.js"
json0.b; // Error (good)
>json0.b : any
>json0 : { a: number; }
>b : any
/** @type {{ b: number }} */
const js1 = require("./js.js"); // Error (good)
>js1 : { b: number; }
>require("./js.js") : { a: number; }
>require : any
>"./js.js" : "./js.js"
js1.b;
>js1.b : number
>js1 : { b: number; }
>b : number
=== /json.json ===
{ "a": 0 }
>{ "a": 0 } : { a: number; }
>"a" : number
>0 : 0
=== /js.js ===
module.exports = { a: 0 };
>module.exports = { a: 0 } : { a: number; }
>module.exports : { a: number; }
>module : { exports: { a: number; }; }
>exports : { a: number; }
>{ a: 0 } : { a: number; }
>a : number
>0 : 0