0

I am calling a function that appears to obviously be a function, but I keep getting TypeError: [function name] is not a function.

Here is a minimal example to reproduce the error.

main.ts

import someFunction from './someFunction'
export const baseUrl = document.location.protocol + '//' + document.location.hostname

someFunction(); //causing TypeError: someFunction is not a function

someFunction.ts

import {Foo} from './Foo'
export default function someFunction(): void {
    //some code here
    let foo = new Foo();
    //some other code here
}

Foo.ts

import {baseUrl} from './main'
export class Foo{
    constructor()

    private someRandomPrivateFunction(): void {
        //some code
        let url = baseUrl + "other/stuff"; //removing this line fixes the TypeError
        //some other code
    }
}

Some details on the background items being used.

Typescript is 1.8 targeting ES5, and generating modules using AMD.

RequireJS is 2.2.0 and data-main points to main

I was testing in Chrome 52.0.2743.116 m

TJ Rockefeller
  • 3,178
  • 17
  • 43

1 Answers1

1

This took me way too long to figure out, but eventually it boiled down to a circular reference.

In Foo.ts if the reference to baseUrl is removed, then everything else works fine because baseUrl is a dependency coming from main.ts

To fix the problem, baseUrl just needed to be moved to a different file baseUrl.ts

Some painful lessons drawn from this experience:

  1. Nothing should be dependent on main.ts... that one should have been obvious.
  2. The error messages spawning from a circular reference can be very vague, completely unrelated, and several layers away from the actual problem, so don't depend on error messages to avoid circular dependencies.
TJ Rockefeller
  • 3,178
  • 17
  • 43
  • Glad you fixed it on your end. Just wanted to chime in and say I tried your example above and was not seeing the `TypeError` you were seeing. – mrand01 Aug 11 '16 at 03:16
  • @mrand01 I added some details to the question that should help mimic what I was doing more accurately. Also, it may be worth noting that the `TypeError` was only at run time in Chrome. There were no indications of an error until I tried to run it. – TJ Rockefeller Aug 11 '16 at 13:21