What's the correct way to use requireJS with typescript?

前端 未结 4 2028
闹比i
闹比i 2020-12-25 11:29

The examples I have found here and here say to use module(). However, when I compile I get \"warning TS7021: \'module(...)\' is deprecated. Use \'require(...)\' instead.\"

4条回答
  •  北海茫月
    2020-12-25 12:06

    For :

    When using typescript and requireJS, how do I access a class in one .ts file from another .ts file where requireJS will load the second file and give me the class in the first file? Is there a way to do the standard requireJS approach with two .ts files where the define() at the top loads the second ts file and returns back the object it builds at the end?

    simply :

    // from file a.ts
    export class Foo{
    
    }
    
    // from file b.ts
    // import 
    import aFile = require('a')
    // use: 
    var bar = new aFile.Foo();
    

    and compile both files with --module amd flag.

    For :

    Sort-of the same as question #2. From a java script file, can I use the define() construct on a type script file to get the instantiated object? If so, how?

    To use a.ts from b.js simply :

    // import as a dependency:
    define(["require", "exports", 'a'], function(require, exports, aFile) {
    
        // use:
        var bar = new aFile.Foo();
    });
    

    This is similar to what you would get if you compile b.ts

提交回复
热议问题