Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 如何向模块中的窗口对象添加类型化属性?_Typescript_Window_Global_Browserify_Tsify - Fatal编程技术网

Typescript 如何向模块中的窗口对象添加类型化属性?

Typescript 如何向模块中的窗口对象添加类型化属性?,typescript,window,global,browserify,tsify,Typescript,Window,Global,Browserify,Tsify,我目前有一个文件main.ts,代码如下: import Game from './game'; declare global { interface Window { game: Game; throttleScreenResizeRender: number; resizeTimeout: number; } } ... window.throttleScreenResizeRender = 0; 这很好,但我正在尝试

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

import Game from './game';

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

 ...

window.throttleScreenResizeRender = 0;
这很好,但我正在尝试将窗口类型放在一个单独的文件中,以避免代码重复。我当前的尝试如下所示:

// window.d.ts

import Game from './game';

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

这给了我一个
错误TS2339:类型“Window”上不存在属性“throttleScreenResizeRender”。
编译时

我使用的是Typescript版本2.0.3

我怎样才能做到这一点

编辑

我正在使用Browserify和tsify,如所示。这是我的吞咽文件:

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"
  },

您使用的是哪个版本的typescript?使用
1.8.10
2.0.3
对我来说似乎工作得很好。你确定这正是你要编译的代码吗?我使用的是2.0.3版。对不起,是的,这正是代码。我工作得很好,唯一的区别是我没有游戏模块。可能尝试添加
export{}在您的
窗口中。d.ts
?也不工作。。。即使我从window.d.ts中删除导入和导出并声明游戏为any,将其转换为脚本…您使用的是哪个版本的typescript?使用
1.8.10
2.0.3
对我来说似乎工作得很好。你确定这正是你要编译的代码吗?我使用的是2.0.3版。对不起,是的,这正是代码。我工作得很好,唯一的区别是我没有游戏模块。可能尝试添加
export{}在您的
窗口中。d.ts
?也不工作。。。即使我从window.d.ts中删除导入和导出并声明游戏为any,将其转换为脚本。。。
  "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"
  },