简体   繁体   中英

Can't get “crossroads” to load as requirejs AMD module in typescript

I am trying to port a javascript app to typescript that uses requirejs. I am using the typedefinition from @mhegazy at DefinitelyTyped: https://github.com/borisyankov/DefinitelyTyped/tree/master/crossroads Typescript compiler is giving me the following error...

error TS2307: Cannot find external module 'crossroads'.

This did all work in javascript using "defines" instead.

I just used this library as a complete example. What am I missing? Documentation is hard to dig up if TSC is really even paying attention to a requirejs config. I know crossroads depends on js-signals. Is there a problem with definitions and ts files being in different directories? From what I have read here I should not have to even put the reference comment. But if I then try to use a relative path, it cannot find it either.

Here is my app structure

  • index.html

Application ts/js

  • app/config.ts
  • app/bootup.ts
  • app/someclass.ts

Typescript definitions

  • typings/crossroads/crossroads.d.ts
  • typings/requirejs/require.d.ts

Javascript libraries

  • bower_components/crossroads/dist/crossroads.js
  • bower_components/requirejs/require.js

index.html has this script include

<script data-main="app/config" src="bower_components/requirejs/require.js>
</script>

/app/config.ts:

/// <reference path="../typings/requirejs/require.d.ts" />
(function () {
    requirejs.config({
        baseUrl: ".", 
        paths: { 
            "crossroads": "bower_components/crossroads/dist/crossroads"
        }
    })
    require(["app/bootup"]);
}) ();

/app/someclass.ts:

/// <reference path="../typings/crossroads/crossroads.d.ts" />
import crossroads = require("crossroads");

class SomeClass{
 // do something with crossroads
}

import crossroads = require("crossroads");

To use import/require you either need:

  • relative path pointing to a TypeScript file.
  • a declared external module.

In your case you need to declare an external module for TypeScript to know about it. Eg:

declare module 'crossroads'{
    export = crossroads;
} 

UPDATE I've updated the definitions so you don't need to do this : https://github.com/borisyankov/DefinitelyTyped/pull/3866 Enjoy!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM