最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Importing a typescript file without using any of the exports - Stack Overflow

programmeradmin3浏览0评论

I'm pretty new to TypeScript and I have a main.tc where I import items.tc, which contains and exports some item classes, and also adds the class types to an array from itemManager.tc. The problem is that they only get added to the array if I use atleast 1 export from items.tc in some way in main.tc, even just logging an export works.

Here is some shortened versions of my files:

main.tc

import * as Items from "./item";
import * as ItemManager from "./itemManager";

console.log(ItemManager.list) //List is empty

items.tc

import * as ItemManager from "./itemManager";

export class Item1
{
    constructor()
    {

    }
}
ItemManager.list.push(typeof Item1);

itemManager.tc

export const list = [];

If I added console.log(Items.Item1) to my main.tc, ItemManger.list will contain all the items, but doing that is not ideal. Is there a way to make sure an imported file gets run? or am I supposed to use something else than importing?

I'm using webpack to compile it to a single .js file, I don't know if that changes anything.

I'm pretty new to TypeScript and I have a main.tc where I import items.tc, which contains and exports some item classes, and also adds the class types to an array from itemManager.tc. The problem is that they only get added to the array if I use atleast 1 export from items.tc in some way in main.tc, even just logging an export works.

Here is some shortened versions of my files:

main.tc

import * as Items from "./item";
import * as ItemManager from "./itemManager";

console.log(ItemManager.list) //List is empty

items.tc

import * as ItemManager from "./itemManager";

export class Item1
{
    constructor()
    {

    }
}
ItemManager.list.push(typeof Item1);

itemManager.tc

export const list = [];

If I added console.log(Items.Item1) to my main.tc, ItemManger.list will contain all the items, but doing that is not ideal. Is there a way to make sure an imported file gets run? or am I supposed to use something else than importing?

I'm using webpack to compile it to a single .js file, I don't know if that changes anything.

Share Improve this question edited Jan 10, 2017 at 21:08 Thomas asked Jan 7, 2017 at 2:39 ThomasThomas 731 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 23

If you have a named import and the name is not used, the import is ignored.

For imports that have side-effects - and no names that you wish to use - you can use this syntax:

import "./some-module-with-side-effects";

From the documentation:

Import a module for side-effects only

Though not recommended practice, some modules set up some global state that can be used by other modules. These modules may not have any exports, or the consumer is not interested in any of their exports. To import these modules, use:

import "./my-module.js";
发布评论

评论列表(0)

  1. 暂无评论