Bring in jspm libraries to your project via typescript

While researching how to import jspm packages into typescript, I noticed that most examples assumed the use of SystemJS for loading and interpreting them in the browser. However, I prefer using tsc to compile commonjs modules and only import the js code, as it seems to be a more general and error-resistant approach.

My directory structure is as follows:

src/index.ts
jspm_packages/...
config.js
tsconfig.json

The contents of tsconfig are:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "noEmitOnError": true,
    "noImplicitAny": false,
    "rootDir": "src",
    "outDir": "target/app",
    "sourceMap": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "declaration": true
  },
  "exclude": [
    "jspm_packages",
    "node_modules",
    "typings",
    "target"
  ]
}

For testing purposes, I installed Angular 2 with jspm install npm:angular2 and attempted to import it in my index.ts using

import { bootstrap } from 'angular2/platform/browser';

However, when running tsc, I encountered the error:

src/index.ts(1,27): error TS2307: Cannot find module 'angular2/platform/browser'.

I'm curious if there's a way to make jspm packages known to typescript. I've tried various approaches such as removing jspm_packages from the tsconfig exclude list, switching to node module resolution, or systemjs module generation. Perhaps there's a specific combination I haven't discovered yet. Any suggestions on what to try next?

Answer №1

I have encountered the same challenge and upon conducting some investigation, it appears that there are currently no effective solutions available to address this issue.

Possible Solutions:

A) One workaround is to duplicate your dependencies and install them using npm. This should prevent any errors from being thrown by tsc as it resolves from npm.

B) Modify your tsconfig.json file to map angular to the jspm path. For instance:

"baseUrl": ".",
"paths": {
     "angular2/*": [
        "jspm_packages/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ec8d828b99808d9edeacdec2dcc2dcc18e89988dc2db">[email protected]</a>/*"
     ],
     "rxjs/*": [
        "jspm_packages/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="45373d2f3605706b756b7568272031246b77">[email protected]</a>/*"
     ]
  }

Refer to https://github.com/frankwallis/plugin-typescript/tree/master/examples/angular2 for a comprehensive example.

Please note that "baseUrl" and "paths" are unofficial properties and are only available in the beta version of the typescript compiler.

The progress of addressing this issue can be monitored at: https://github.com/Microsoft/TypeScript/issues/6012

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Does the class effectively implement the interface even if the method of a member variable has undefined arguments?

Let's take a closer look at my code, which lacks proper descriptions. Here is the interface: interface IModel<T = any> { effects: { [key: string]: (getState: () => T) => void; }; } interface IState { name: string; age: numbe ...

Extending Enums in Typescript: A Comprehensive Guide

How can you work with a list of constants or Enum? Here is an example: enum MyList { A, B } enum MyList2 { C } function process<T>(input:MyList | T):void { } process<MyList2>(123) // The compiler does not recognize that 123 ...

What confusion do I have regarding resolving promises in Angular's state management?

Here is the state defined in appRouteConfig.js, where I am using $promise to verify userList: .state('userAccounts',{ url:'/userAccounts', controller:'UserAccount', resolve:{ registerService: "registerServ ...

The pagination feature in ng-table is not working as expected. Instead of displaying paginated table content

I am currently working on implementing pagination with ng-table, but I am facing an issue where all the data is being displayed on a single page instead of paginating it. Although there are no errors and the page numbers are shown, the content is not being ...

Angular integrated with DOJO application

I'm currently working on incorporating Angular into an existing DOJO application. I've set up a plunkr http://plnkr.co/edit/8pgdNwxFoxrDAQaQ4qfm?p=preview, but unfortunately, the angular module is not loading. Can anyone provide guidance on what ...

Mobile website scroll assistant

Seeking a solution to aid mobile users in scrolling through a lengthy article page. Typically in mobile apps, an alphabetical index assists users in navigating long lists. How can I incorporate a similar feature into a webapp? For context, my tech stack i ...

The error TS2339 is indicating that there is no property called myProperty on the type SetStateAction<User>

I'm encountering a TypeScript error while working with React that's leaving me puzzled: <html>TS2339: Property 'subEnd' does not exist on type 'SetStateAction&lt;User&gt;'.<br/>Property 'subEnd' d ...

Exploring the power of NestJS integration with Mongoose and GridFS

I am exploring the functionality of using mongoose with NestJs. Currently, I am leveraging the package @nestjs/mongoose as outlined in the informative documentation. So far, it has been functioning properly when working with standard models. However, my p ...

Issue: The key length and initialization vector length are incorrect when using the AES-256-CBC encryption algorithm

Within my coding project, I have developed two essential functions that utilize the AES-256-CBC encryption and decryption algorithm: import * as crypto from "crypto"; export const encrypt = (text: string, key: string, iv: string) => { con ...

Find the total of values in an array that may be null or undefined

In this scenario, I have an array that looks like this: myData = [[2, null, null, 12, 2], [0, 0, 10, 1, null], undefined]; The goal is to calculate the sum of each sub-array, resulting in an array like this: result = [16, 11, 0]. The ...

Typescript sometimes struggles to definitively determine whether a property is null or not

interface A { id?: string } interface B { id: string } function test(a: A, b: A) { if (!a.id && !b.id) { return } let c: B = { id: a.id || b.id } } Check out the code on playground An error arises stating that 'objectI ...

What can be done to avoid sending repeated requests to the server?

I am working on a controller and service where I have implemented caching for values. app.service('nameService', ['$q','$http', function($q, $http){ var that = this; that.name = null; this.index = function(){ ...

Tips for accessing a URL page in Ionic 3 without using the ionic-native plugin

Is there a method to open a specific page when a particular URL is accessed by the browser, without relying on ionic-native for deep linking? This functionality would be beneficial both for the app itself and for development purposes. For instance, can h ...

Issues with AngularJS 1.6.0 routes are causing problems at the moment

When searching through Stackoverflow, I was surprised not to find this question asked already. It seems like a common issue, yet I am struggling with it alone. In my current project, the routes are not functioning correctly despite double-checking everyth ...

Transforming numbers into arrays in JavaScript/TypeScript for Angular 7

What is the best way to convert the number 10 into an array in JavaScript? expected output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] OR How can I transform the number 10 into the number of items within an array using JavaScript? ...

The componentWillReceiveProps method is not triggered when a property is removed from an object prop

Just starting out with React, so I could really use some assistance from the community! I am working with a prop called 'sampleProp' which is defined as follows: sampleProp = {key:0, value:[]} When I click a button, I am trying to remo ...

Encountering error "An import path cannot end with a '.ts' extension." when importing TypeScript file in Vue Single File Component (SFC) within VS Code

Currently, I am in the process of transitioning my .vue components from using JavaScript to TypeScript. As a result, my single file components are structured as follows: <template> ...something... </template> <script lang="ts"> import ...

JavaScript: Manipulating Data with Dual Arrays of Objects

//Original Data export const data1 = [ { addKey: '11', address: '12', value: 0 }, { addKey: '11', address: '12', value: 0 }, { addKey: '12', address: '11', value: 0 }, { addKey: &a ...

I am attempting to trigger a mouseup event following a mousedown action

elements[0].onmousedown = function(){ console.log('screen clicked.'); triggerMouseUp(); }; I just need to incorporate a function in my code that simulates mouseup event, even when the user is still holding down the click button. e ...

The class names in Material-UI seem to constantly shuffle whenever I refresh the page

Right now I am experimenting with Text Fields in Material-UI and my goal is to make them smaller in size. For reference, here is an example of the class name: .eKdARe.header .input-container input After making changes in my file and refreshing the page, ...