Reputation: 42229
I'm having trouble trying to import types in my TypeScript project. I'm not entirely sure how import works...
File foo.ts
module Foo {
export class Bar {
}
}
File bar.ts
import { Bar } from "Foo";
Cannot find module 'Foo'.
import { Bar } from "../scripts/Foo";
File '/scripts/foo.ts' is not a module.
So, what am I misunderstanding here? Also, should I be using module
or namespace
, and what is the difference?
Note foo.ts
and bar.ts
are in the same location...can I omit ../scripts/
?
Upvotes: 1
Views: 598
Reputation: 950
The thing you are missing is that TypeScript supports two types of modules - external and internal. Internal modules were renamed to namespaces in newer versions (that's the reason there is older module
and newer namespace
keywords - you can use them as you wish in your project / they are equal).
When you are using import
syntax in bar.ts you are trying to consume external module.
But foo.ts is not external module until you add export
into root scope.
I'd recommend to stay with import
and use external modules like this:
Foo.ts
export class Bar {
}
Bar.ts
import { Bar } from "./Foo"; // path needs to be relative here
// if referencing files are in same folder use ./ to force the path to be relative
There is no need to use module
/namespace
until you want to wrap classes into namespaces.
Upvotes: 1
Reputation: 74
You should probably read some of the documentation. A quick google gave me this, which explains the different between namespace and module. Take a look at that and try to get a better view of exactly what it is that you need because I cannot exactly tell by your post because it seems syntax for modules would be declare module "Foo" {...}
and based on what I'm seeing there it seems namespace may be what you want. I'm not entirely sure but I feel like your answer is on that page.
Upvotes: 0