Reputation: 1452
Contents of foo.ts:
let a: Person = new Person();
Contents of bar.ts:
class Person {}
The tsconfig.json file contains the default set of values created by tsc --init
.
I'm using typescript version 2.6.2.
The code above simply compiles with no errors about Person
being not defined. If I rename Person
to Persons
in foo.ts, then it does throw an error saying cannot find name 'Persons'
. So it's as if it is automatically importing this file somehow.
Also note that I'm not exporting the Person
class in bar.ts. If I do add an export statement, then everything behaves as it should - I get an error saying cannot find name 'Person'
.
Does anyone know what's going on here? Is this a user error?
Edit:
Maybe this makes sense in the browser, but to me this behavior makes no sense in a node.js application. Is there a Typescript flag which forbids this behavior?
Upvotes: 12
Views: 3699
Reputation: 1452
The compiler flag --isolatedModules
will ensure that all .ts files are modules. Once you make foo.ts
and bar.ts
modules by adding at 1 import
or export
, then the Person
class will not be global anymore.
This, however doesn't solve the following similar issue: https://github.com/Microsoft/TypeScript/issues/18232#issuecomment-359200157
Upvotes: 3
Reputation: 692131
Your bar.ts is not a module, since it doesn't have any import nor any export. So it defines a global Person class, that doesn't need to be imported, since it's global. Export it, and TypeScript will complain about the missing import.
Upvotes: 4