Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure `baseUrl` for typescript compiler?

Tags:

typescript

I have a website built with modules using requireJS. I want to start using TypeScript (1.8, Visual Studio 2015) on this project, and trying to rewrite one of the modules.

Presently my module looks as follows:

// file: ProjectFoo/app/dashboard/viewProfile.js
define(["services/datasvc"], function (datasvc) {
    datasvc.getData(...).then(...);
});

File datasvc.js is located at a following path: ProjectFoo/services/datasvc.js

To make datasvc.js accessible to requireJS, I have configured a following path (requireJS configuration):

services -> ./services

After rewriting my module, I use following construct to import datasvc (note: TypeScript compiler is configured to use amd module structure).

import * as datasvc from "services/datasvc";

However that does not work, since TypeScript treats services/datasvc as a path relative to the current file. However if I rewrite that import statement as follows, then TypeScript [compiler] succeeds:

import * as datasvc from "../../services/datasvc";

However this results in datasvc being imported using relative path, i.e. generate code looks something like:

define(["require", "exports", "../../services/datasvc"]...

The problem here is that I will have to change number of ../ to include in the module path, depending on the depth of nesting of the file I am in.

Question: Is there a way to use import * as datasvc from "services/datasvc" and somehow configure TypeScript to handle that same way requireJS does?

like image 777
THX-1138 Avatar asked Jun 27 '16 16:06

THX-1138


People also ask

How do I find the base URL in TypeScript?

We create the a element with document. createElement . Then we set the href of it to the URL we want to extract the base URL from. Then we can get the baseURL by combining the protocol and hostname properties.

What is TSC command in TypeScript?

Running tsc locally will compile the closest project defined by a tsconfig.json , you can compile a set of TypeScript files by passing in a glob of files you want. sh. # Run a compile based on a backwards look through the fs for a tsconfig.json. tsc. # Emit JS for just the index.ts with the compiler defaults.

What is TSC TypeScript compiler?

TypeScript is a superset of Javascript and supports the Object-Oriented Concept. In TypeScript, whenever we compile our code of file type . ts it gets converted into a . js file by using tsc compiler. It means that the TypeScript code get trans-compiled into plain JavaScript.


1 Answers

You'll need typescript 2.0 or above in order to configure a base url.

As outlined in the release notes you can then create a tsconfig.json file with the following contents:

{
  "compilerOptions": {
    "baseUrl": "./modules"
  }
}

Now imports to "moduleA" would be looked up in ./modules/moduleA

import A from "moduleA";

Side note: Many developers rely on gulp or similar tools to compile typescript and are not using the typescript compiler directly. If you rely on one of these tools you have to make sure that your tool is picking up these new options like baseUrl from tsconfig.json and pass it to the typescript compiler.

like image 104
ben Avatar answered Oct 01 '22 23:10

ben