module - Typescript, Requirejs, import statement and aliases -


with java, import easy , clear.

you import following statement :

import fr.domain.myutils; 

then can use this:

myutils.mystaticmethod(); 

you need namespace myutils if there 2 in same file.

with typescript amd , requirejs seems more complicated.

here import statement:

import u = require('fr/domain/myutils'); 

and way use it:

u.fr.domain.myutils.mystaticmethod(); 

quite verbose...

the way found fare use alias double import statement:

import u = require('fr/domain/myutils'); import myutils = u.fr.domain.myutils; 

after doing can write in module:

myutils.mystaticmethod(); 

it's cleaner eclipse ts plugin lost , auto completion become erratic. in visual studio auto completion ok "f12 go definition" has done twice annoying.

is there better way ? or should keep namespaces short can ?

you’re doing wrong.

your 'fr/domain/myutils' module should exporting whatever supposed myutils. i.e. should this:

export function mystaticmethod() { /* ...code... */ } 

it should not exporting global namespace object, , should not adding global namespace object somewhere else. natural placement of module files in directories is way create “namespaces” when work external modules.

if correctly consumer looks this:

import myutils = require('fr/domain/myutils'); myutils.mystaticmethod(); 

or, more using es module syntax:

import { mystaticmethod } 'fr/domain/myutils'; mystaticmethod(); 

Comments