如何将类型属性添加到模块中的窗口对象?

时间:2016-10-23 18:08:48

标签: typescript window global browserify tsify

我目前有一个文件main.ts,代码如下:

import Game from './game';

declare global {
    interface Window {
        game: Game;
        throttleScreenResizeRender: number;
        resizeTimeout: number;
    }
}

 ...

window.throttleScreenResizeRender = 0;

这很好用,但是我试图将Window类型放在一个单独的文件中,以避免代码重复。我目前的尝试看起来像这样:

// window.d.ts

import Game from './game';

declare global {
    interface Window {
        game: Game;
        throttleScreenResizeRender: number;
        resizeTimeout: number;
    }
}

// main.ts

import Game from './game';
import 'interfaces/window';

window.throttleScreenResizeRender = 0;

这给我一个Error TS2339: Property 'throttleScreenResizeRender' does not exist on type 'Window'.编译。

我使用的是Typescript版本2.0.3

我怎样才能做到这一点?

编辑

我正在使用带有tsify的Browserify,如the handbook所示。这是我的gulp文件:

const gulp = require("gulp");
const browserify = require("browserify");
const source = require('vinyl-source-stream');
const tsify = require("tsify");

...

gulp.task('tsify', function () {
    return browserify({
        basedir: '.',
        debug: true,
        entries: ['src/main.ts'],
        cache: {},
        packageCache: {}
    })
    .plugin(tsify)
    .bundle()
    .pipe(source('bundle.js'))
    .pipe(gulp.dest(destinationPath));
});

相关packages.json

  "dependencies": {
    "gulp": "^3.9.1",
    "gulp-typescript": "^3.0.2",
    "typescript": "^2.0.3"
  },
  "devDependencies": {
    "browserify": "^13.1.0",
    "gulp": "^3.9.1",
    "gulp-typescript": "^3.0.2",
    "tsify": "^2.0.2",
    "typescript": "^2.0.3",
    "vinyl-source-stream": "^1.1.0"
  },

0 个答案:

没有答案