0

In my Typescript I am using the import function as described here:

https://github.com/Microsoft/TypeScript/issues/12933

So my code is like:

 import("../myScriptToBeImported").then((module) => {
                this.dosomethingWithModule(module);
            }).catch(this.doSomethingWithError.bind(this));

I am running this as part of an MVC project in Visual Studio with Webpack. Webpack compiles the Typescript without any errors and the project runs fine, yet for the above line the following errors are always shown in Visual Studio:

  • TS1128 (TS) Declaration or statement expected.
  • TS2391 (TS) Function implementation is missing or not immediately following the declaration.
  • TS7010 (TS) 'import', which lacks return-type annotation, implicitly has an 'any' return type.

I am using Typescript version 2.4.2. How can I get rid of these errors?

For information, my tsconfig file looks like this:

{
  "compilerOptions": {
    "module": "esnext",
    "moduleResolution": "node",
    "noEmitOnError": true,
    "strict": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [ "es6", "dom" ],
    "baseUrl": ".",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  },
  "exclude": [
    "node_modules",
    "wwwroot"
  ],
  "compileOnSave": false,
  "buildOnSave": false
}

1 Answer 1

1

Function-style import isn't part of the JS module spec yet. You need to set module to "esnext" in your tsconfig.json to use it.

If you've already done that, the issue may lie with Visual Studio - make sure your project is configured to use the correct version of the TypeScript compiler.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi Joe, the module is already set to "esnext" in the tsconfig.json file. I have edited the question so you can see the whole tsconfig file.
Hm, strange! That makes me think that either a) VS is not picking up the tsconfig.json file, or b) VS is using a different version of the TypeScript compiler.
Aha - on the project properties, Typescript Build tab, the Typescript version was set to 2.3, so I changed this to 'Latest Typescript Version'. This didn't make any difference until I restarted Visual Studio, and then the errors disappeared on build. If you add to your answer I will mark as correct.
Oh heck - the errors have come back. No idea why - I did a full build before and they didn't show, but now they are there again! Any other ideas?
Hm, not sure, I'm afraid ): Might be worth rasing it on the TypeScript GitHub.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.