Typescript: "use strict" and source map are emitted to json files that are imported as a module

Created on 7 Jun 2018  路  19Comments  路  Source: microsoft/TypeScript

TypeScript Version: 3.0.0-dev.201xxxxx

Search Terms: json use strict

the "use strict" and the source map in the emitted json cause errors during runtime....

Duplicate

Most helpful comment

This is happening for me on 3.6.2, removing isolatedModules fixed it for me.

All 19 comments

Duplicate of https://github.com/Microsoft/TypeScript/issues/24514.

Fix should be in typescript@next today, and in [email protected] next week.

sorry, I did not see it fixed in typescript@next then and after just updating to 2.9.2 now it also still persists.

i am unable to reproduce this locally on [email protected]. can you please share repro steps?

hmm...
I just have something like import setup from "./setup.json"; in a .ts file and the new compiler option resolveJsonModuleset to true.
The resulting copy of the imported json file is emitted with the use strict and sourcemap - just as before.

in case there is a conflict with some other compiler option, here is my config:

{
    "compilerOptions": {
        "module": "commonjs",
        "strict": true,
        "strictPropertyInitialization": false,
        "noUnusedParameters": true,
        "removeComments": false,
        "preserveConstEnums": true,
        "sourceMap": true,
        "target": "ES2017",
        "outDir": "./compiledJS",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "resolveJsonModule": true,
        "esModuleInterop": true,
        "pretty": true,
        "plugins": [
            {
                "name": "tslint-language-service",
                "alwaysShowRuleFailuresAsWarnings": false,
                "ignoreDefinitionFiles": true,
                "supressWhileTypeErrorsPresent": false,
                "mockTypeScriptVersion": false
            }
        ]
    },
    "exclude": [
        "node_modules",
        "compiledJS",
        "playground",
        "documentation",
        "unitTesting",
        "gulpfile.js",
        "lib",
        "bundle.js",
        "**/*/*.quokka.ts"
    ]
}
c:\test\24733>type tsconfig.json
{
    "compilerOptions": {
        "module": "commonjs",
        "strict": true,
        "strictPropertyInitialization": false,
        "noUnusedParameters": true,
        "removeComments": false,
        "preserveConstEnums": true,
        "sourceMap": true,
        "target": "ES2017",
        "outDir": "./compiledJS",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "resolveJsonModule": true,
        "esModuleInterop": true,
        "pretty": true,
        "plugins": [
            {
                "name": "tslint-language-service",
                "alwaysShowRuleFailuresAsWarnings": false,
                "ignoreDefinitionFiles": true,
                "supressWhileTypeErrorsPresent": false,
                "mockTypeScriptVersion": false
            }
        ]
    },
    "exclude": [
        "node_modules",
        "compiledJS",
        "playground",
        "documentation",
        "unitTesting",
        "gulpfile.js",
        "lib",
        "bundle.js",
        "**/*/*.quokka.ts"
    ]
}

c:\test\24733>type a.ts
import x = require("./a.json")
x.n;
x.a

c:\test\24733>type a.json
{
    "a": 0,
    "n": "s",
}

c:\test\24733>tsc --v
Version 2.9.2

c:\test\24733>tsc

c:\test\24733>type .\compiledJS\a.json
{
    "a": 0,
    "n": "s"
}

the only difference I see: you wrote import x = require("./a.json") where I did import x from "./a.json";
Everything works fine in my case too - besides the mentioned problem...

c:\test\24733>type a.ts
import x from "./a.json";
x.n;
x.a

c:\test\24733>tsc --v
Version 2.9.2

c:\test\24733>tsc

c:\test\24733>type .\compiledJS\a.json
{
    "a": 0,
    "n": "s"
}

wait a sec. seems the jsons just dont get overwritten if they already exist in the output folder!

nope. if I use the import from syntax it does not work for me. only with the import x=require....
strange....

can you provide a repro project we can look at?

i literally just used the same files as you in an empty project just now....

are u sure you are using 2.9.2?

yeah sure.... tsc --v ;-)

ok.. I did uninstall and reinstall typescript. now it works _regardless_ of the syntax.
actually I had a similar case happen long time ago when updating from TS 2.1...

sorry to have wasted your time! cheers, marcel

I guess the issue can be closed. Thank you for your help!

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

Why I'm facing this issue with "typescript": "^3.4.1"?!

This is happening for me on 3.6.2, removing isolatedModules fixed it for me.

ran into this using "typescript": "^3.7.5". confirmed removing isolatedModules works

The isolatedModules workaround is apparently no longer needed in TypeScript 3.8.1 and above.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bgrieder picture bgrieder  路  3Comments

wmaurer picture wmaurer  路  3Comments

seanzer picture seanzer  路  3Comments

Roam-Cooper picture Roam-Cooper  路  3Comments

jbondc picture jbondc  路  3Comments