Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

External module without import statement in Typescript

The moment you add an import statement to your Typescript file, this file is considered an external module. So this is not a problem:

File.1.ts

import { Type } from '...';
let whatever = 123;
...

File.2.ts

import { Type } from '...';
let whatever = 234;
...

So this works. But the moment one removes those two import statements, these files aren't considered modules any more and both variables with the same name become global interfering with each other.

Question

How does one force modularization of a source file that doesn't have any import statements?

like image 267
Robert Koritnik Avatar asked Dec 23 '16 09:12

Robert Koritnik


People also ask

How do I declare a module in TypeScript?

A module can be created using the keyword export and a module can be used in another module using the keyword import . In TypeScript, files containing a top-level export or import are considered modules. For example, we can make the above files as modules as below. console.

How do I import a custom module in TypeScript?

The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum. When exporting a module using export = , TypeScript-specific import module = require("module") must be used to import the module.

How can we access a class of module from outside in TypeScript?

In typescript by using an import statement, a module can be utilized in another module. any variables, classes, methods, or other objects declared in a module are not accessible outside of it. The keyword “export” may be used to construct a module, and the term “import” can be used to import it into another module.


1 Answers

It is required to import or export something. I suggest to export an undefined value:

export let undefined;

The compiler generates no code for this instruction.

like image 176
Paleo Avatar answered Nov 15 '22 06:11

Paleo