[TypeScript] Using Typings and Loading From node_modules

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[TypeScript] Using Typings and Loading From node_modules相关的知识,希望对你有一定的参考价值。

Using TypeScript when installing packages from npm often requires you to install related definition files. This lesson shows you how to use typings to install es6-shim then how to configure SystemJS to load from node_modules.

 

Install:

npm install --save rxjs

 

Import:

import {Observable} from ‘rxjs/Observable‘;
import ‘rxjs/add/observable/interval‘;

But you will find errors, the reason for that rxjs include es6, but our target is es5.

 

So, install:

typings install es6-shim --save -ambient

 

Include:

<script>
    System.config({
        packages: {
            "dist": {
                "defaultExtension": "js",
                "main": "main"
            },
            "rxjs": {
                "defaultExtension": "js"
            }
        },
        map: {
            "lodash": "https://npmcdn.com/[email protected]",
            "rxjs": "node_modules"
        }
    });

    System.import("dist")
</script>

 

Then you can start using rxjs;

import {Observable} from ‘rxjs/Observable‘;
import ‘rxjs/add/observable/interval‘;

Observable.interval(1000)
    .subscribe( x => console.log(x))

 

以上是关于[TypeScript] Using Typings and Loading From node_modules的主要内容,如果未能解决你的问题,请参考以下文章

React Native Typescript Typing for "Backwards" ScrollView

[TypeScript] Dynamically initialize class properties using TypeScript decorators

[TypeScript] Using Interfaces to Describe Types in TypeScript

[TypeScript] Using Assertion to Convert Types in TypeScript

[TypeScript] Using Lodash in TypeScript with Typings and SystemJS

[TypeScript] Type check JavaScript files using JSDoc and Typescript 2.5