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.
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.
With TypeScript 3.8, you can import a type using the import statement, or using import type .
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 .
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With