S
S
sanex33392016-02-12 15:42:41
Angular
sanex3339, 2016-02-12 15:42:41

Typescript - how to correctly set aliases for modules?

Playing with Angular and Typescript.
There is a scripts/ folder which contains the main component of Angular, as well as the Services/ and Components/ folders.
The Services/ and Components/ folders contain services and directives, respectively.
Now, in order for me to import a service from a component, I have to import along the view path

import { SomeService } from '../../Services/SomeService';

How can I make an alias for the Services folder so that the import looks like
import { SomeService } from 'services';
So far, I have created 2 files:
services.d.ts and components.d.ts
with content (using services.d.ts as an example):
declare module 'services' {
    export { FileUploadService } from './Services/FileUploadService';
    export { RedirectService } from './Services/RedirectService';
}

But then, I get errors like
resources/assets/typescript/components.d.ts(2,5): error TS2439: Import or export declaration in an ambient module declaration cannot reference module through relative module name.
resources/assets/typescript/components.d.ts(3,5): error TS2439: Import or export declaration in an ambient module declaration cannot reference module through relative module name.
resources/assets/typescript/components.d.ts(3,33): error TS2307: Cannot find module './Components/UI/ProgressBar/ProgressBar'.
resources/assets/typescript/services.d.ts(2,5): error TS2439: Import or export declaration in an ambient module declaration cannot reference module through relative module name.
resources/assets/typescript/services.d.ts(2,39): error TS2307: Cannot find module './Services/FileUploadService'.
resources/assets/typescript/services.d.ts(3,5): error TS2439: Import or export declaration in an ambient module declaration cannot reference module through relative module name.
resources/assets/typescript/services.d.ts(3,37): error TS2307: Cannot find module './Services/RedirectService'.

How to do it right?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Kitmanov, 2016-02-13
@k12th

Well, if you load and assemble ts using SystemJS, then you can make as many aliases as you like there. If tsc, then there seems to be no such thing.

M
Maxim Antsiferov, 2019-12-27
@Babayka_od

If tsc is used, then baseUrl: “src/“ can be set in tsconfig.json and then imports like
import foo from “components/foo” will be available.
The components folder is located in src

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question