admin管理员组

文章数量:1401933

I have a reproduction for this issue here.

Essentially I have a TSConfig with

    "module": "None",  

I have two files:

//index.ts

import {foo} from "./other"; 


function main(){
    foo();
}
main();

and

//other.ts

export function foo() {
    console.log("x")
}

I'm expecting this to error, because as I understand it, modules are not allowed.

However, if I compile this with tsc then it compiles without error, and it creates CJS module code.

However, if I compile it with tsc --module none --outfile dist/o.js src/index.ts

Then I get the error I expect:

src/other.ts:1:17 - error TS1148: Cannot use imports, exports, or module augmentations when '--module' is 'none'.

1 export function foo() {
                  ~~~


Found 1 error in src/other.ts:1

What gives?

I have a reproduction for this issue here.

Essentially I have a TSConfig with

    "module": "None",  

I have two files:

//index.ts

import {foo} from "./other"; 


function main(){
    foo();
}
main();

and

//other.ts

export function foo() {
    console.log("x")
}

I'm expecting this to error, because as I understand it, modules are not allowed.

However, if I compile this with tsc then it compiles without error, and it creates CJS module code.

However, if I compile it with tsc --module none --outfile dist/o.js src/index.ts

Then I get the error I expect:

src/other.ts:1:17 - error TS1148: Cannot use imports, exports, or module augmentations when '--module' is 'none'.

1 export function foo() {
                  ~~~


Found 1 error in src/other.ts:1

What gives?

Share Improve this question edited Mar 25 at 1:49 dwjohnston asked Mar 24 at 6:11 dwjohnstondwjohnston 12k39 gold badges117 silver badges218 bronze badges 4
  • Does it work if you lowercase it? – Andy Ray Commented Mar 24 at 6:17
  • @AndyRay doesn't make a difference. Also, in the CLI usage --module None also works, shows the error. – dwjohnston Commented Mar 24 at 6:17
  • When you compile without specifying the module option (i.e., tsc src/index.ts), TypeScript defaults to using the CommonJS module system ( typescriptlang./tsconfig/#module ). This is why your code compiles without error and results in CommonJS module code. – Yash Commented Mar 24 at 9:39
  • @Yash But I have specified the module option - I've specified "None" – dwjohnston Commented Mar 25 at 1:49
Add a comment  | 

1 Answer 1

Reset to default 2

Looking at the only place where that error message is used in the TypeScript codebase, we can see that the condition includes languageVersion < ScriptTarget.ES2015. This is presumably because ECMAScript modules became part of the standard in ES6/ES2015.

When you compile via tsc --module none --outfile dist/o.js src/index.ts, whatever target value you may have in your tsconfig.json is ignored and the default (ES5) is used, which is why this error is visible.

本文标签: TypeScript module quotNonequot does not errorStack Overflow