Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript removes import statements not used in code

I am trying to use Typescript and jspm to make an angular app. The problem is when you want to ensure a .js file loaded, in jspm you have to write an import and it ensures the file will load before running your code. But Typescript removes my import. This is the Typescript code I have written. I have to load angular-new-router then add it to my module dependency.

import angular = require('angular');
import MainController = require('./controllers/MainController');
import NgNewRoute = require('angular-new-router');

console.log(angular.version);

var appModule = angular.module('app', ['ngNewRouter']);
MainController.register(appModule);

export = appModule;

My question: How can I instruct Typescript to not remove my import statement, Or I have to do something else to ensure my router loads?

PS: I compile my typescript code to ES5 with commonjs.

EDIT: This question is not the same as TypeScript: import module with only statements. I have this problem working with third party libraries, so I don't want to change them. Also I am using commonjs pattern so amd-dependency does not fix my problem!

EDIT 2: Another problem is I can not require files other than js modules in my Typescript code.

like image 235
alisabzevari Avatar asked Jun 21 '15 05:06

alisabzevari


People also ask

How do I enable imports in TypeScript?

Use import myFunction from "./myModule" to bring it in. More commonly, TypeScript modules say export myFunction in which case myFunction will be one of the properties on the exported object. Use import { myFunction } from "./myModule" to bring it in.

Does TypeScript support import?

With TypeScript 3.8, you can import a type using the import statement, or using import type .

How do I import files into TypeScript?

To import a class from another file in TypeScript: Export the class from file A , e.g. export class Employee {} . Import the class in file B as import { Employee } from './another-file' . Use the class in file B .

What is TypeScript import type?

import type only imports declarations to be used for type annotations and declarations. It always gets fully erased, so there's no remnant of it at runtime. Similarly, export type only provides an export that can be used for type contexts, and is also erased from TypeScript's output.


1 Answers

How can I instruct Typescript to not remove my import statement, Or I have to do something else to ensure my router loads

You need to use something from the import as a variable e.g

import foo = require('./foo'); 
var bar = foo; // Like this

instead of just :

import foo = require('./foo'); 
var bar:foo; // This will not cause an import in the generated JavaScript
like image 96
basarat Avatar answered Oct 07 '22 00:10

basarat