mr haven
mr haven

Reputation: 1644

Can't import exported functions

I am having strange issues with Typescript when I import things from a file which exports them. Sometimes I will export a function, then import it to another file, then I use the function and it is not a function anymore. When I define the function in the same file, all of a sudden the function is a function?!?!?

Why would a function stop being a function when it is exported? I have had similar problems with classes too.

The hard part of this issue is I can't recreate a simple example because it only happens when I am using some kind of higher level package.

For example, I had a similar issue with sequelize-typescript here: my github issue with typescript-sequelize

Below is some codes showing off the basic issue I'm having with one of the decorators from InversifyJS.

container.ts

import {fluentProvide} from "inversify-binding-decorators";
export const provideSingleton = (identifier: any) => {
    return fluentProvide(identifier)
    .inSingletonScope()
    .done(true);
};

test.service.ts

import {provideSingleton} from './container'
@provideSingleton(TYPES.TEST)
export default class TestService {}

The strangest thing is when I put the provideSingleton in the same file as the TestService, everything works!?!?!

Basically to recreate the issue, simply follow the example from here: inversify-binding-decorators - Using @provideFluent multiple times. However there is an issue with the example, so please see this issue: fluentProvide example needed. The above provideSingleton reflects the changes from that issue. Then you simply import the provideSingleton function from another file instead of defining it in the same like in the example.

Can anyone explain to me what I'm missing? Why oh why would certain exported items not bee seen as the type they are? Is there a step I'm not seeing that NodeJS takes to make the item actually exported and therefore different? Can I force the function to resolve as a function so it can be used as such?

ENV:
NodeJS: 10.9.0
Typescript: 3.0.1
Mac: 10.13.16

Upvotes: 0

Views: 610

Answers (1)

mr haven
mr haven

Reputation: 1644

So it looks like you can get issues like this when NodeJS can't handle a recursive import. I'm not exactly sure how to check you are getting this error other than your symptoms are like what I stated above. Basically the recursion caused my function to not load and therefore undefined is not a function.

It would be easy to notice if you had code like so:

a.ts

import B from './b';
export default class A extends B {}

b.ts

import A from './a';
export default class B extends A {}

In my case, I think my function provideSingleton did not like the file I put it in because of some conflicting code in the file, which all I had was:

import {Container} from 'inversify';
import "reflect-metadata";
import {fluentProvide} from "inversify-binding-decorators";
const container = new Container();
function ProvideSingleton(identifier: any) {
    return fluentProvide(identifier)
           .inSingletonScope()
           .done(true);
}
export {container, ProvideSingleton}

In the end, if this issue comes up, try another file for your function and pay good attention to how the order of the loading happens. Although NodeJS handles recursive imports most of the time, you can still trip it out.

Upvotes: 1

Related Questions