使用Gulp + Browserify时无法找到Typescript命名空间

时间:2017-05-02 17:18:10

标签: typescript namespaces

我一直在项目中使用命名空间,这很好,直到突然找不到这个'Test'命名空间。我错过了什么吗?

Can't find variable: Test in Game.ts

example.ts

namespace Test { 
    export class Example {        
        constructor() {           
            console.log("example");
        }
    } 
}

game.ts

/// <reference path="test/example.ts"/>
namespace Game {
    export class Snippet {    
        constructor() {
            // Test is not found
            let s = new Test.Example();
        }
    } 
}

// Game.Snippet IS found
window.addEventListener("load", () => new Game.Snippet());

更新

仔细观察后,我认为这是我的gulpfile或browserify中的错误。当我使用默认的tsc动作进行编译时,它可以工作!但是当使用browserify时它会失败。我跟着this tutorial

Gulpfile.js

var gulp = require("gulp");
var browserify = require("browserify");
var source = require('vinyl-source-stream');
var tsify = require("tsify");
var uglify = require('gulp-uglify');
var sourcemaps = require('gulp-sourcemaps');
var buffer = require('vinyl-buffer');

gulp.task("build", function () {
    return browserify({
        basedir: '.',
        debug: true,
        entries: ['dev/main.ts'],
        cache: {},
        packageCache: {}
    })
    .plugin(tsify)
    .bundle()
    .pipe(source('main.js'))
    .pipe(buffer())
    .pipe(sourcemaps.init({loadMaps: true}))
    .pipe(uglify())
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest("docs/js"));
});

1 个答案:

答案 0 :(得分:1)

我从您的问题中假设您的文件夹结构如下:

folder structure

基于此,您可以尝试将参考设置为以下

/// <reference path="./test/example.ts"/>

<强>更新

我曾尝试导出名称空间并导入它们。它在离子样本项目中运行良好

<强> example.ts

export namespace Test { 
    export class Example {        
        constructor() {           
            console.log("example");
        }
    } 
}

<强> game.ts

import {Test} from "./test/example"/>
export namespace Game {
    export class Snippet {    
        constructor() {
            // Test is not found
            let s = new Test.Example();
        }
    } 
}