fah*_*cht 41 jsdom typescript typescript2.0
我尝试将现有项目转换为使用Typescript,我在测试设置时遇到了问题.
我有一个用于我的测试的安装文件,用于设置jsdom,以便我的所有DOM交互代码在我的测试期间工作.使用Typescript(ts-node with mocha)我总是得到这样的错误:
Property 'window' does not exist on type 'Global'.
Run Code Online (Sandbox Code Playgroud)
为了防止这种情况,我尝试修补NodeJS.Global接口,如下所示:
declare namespace NodeJS{
interface Global {
document: Document;
window: Window;
navigator: Navigator;
}
}
Run Code Online (Sandbox Code Playgroud)
但这并没有改变任何事情.
如何在NodeJS全局变量上启用这些浏览器属性?
附加功能:
这是我的摩卡setup.ts
:
import { jsdom, changeURL } from 'jsdom';
const exposedProperties = ['window', 'navigator', 'document'];
global.document = jsdom('');
global.window = global.document.defaultView;
Object.keys(global.document.defaultView).forEach((property) => {
if (typeof global[property] === 'undefined') {
exposedProperties.push(property);
global[property] = global.document.defaultView[property];
}
});
global.navigator = {
userAgent: 'node.js',
};
changeURL(global.window, 'http://example.com/');
Run Code Online (Sandbox Code Playgroud)
Ste*_*gin 58
把它放在打字稿文件的顶部
const globalAny:any = global;
Run Code Online (Sandbox Code Playgroud)
然后使用globalAny.
globalAny.document = jsdom('');
globalAny.window = global.document.defaultView;
Run Code Online (Sandbox Code Playgroud)
lle*_*aff 23
除了其他答案,您还可以直接在分配站点投射 global
:
(global as any).myvar = myvar;
Run Code Online (Sandbox Code Playgroud)
小智 13
我这样解决了这个问题...
export interface Global {
document: Document;
window: Window;
}
declare var global: Global;
Run Code Online (Sandbox Code Playgroud)
避免 typecasting any
,它消除了打字的目的。而是安装所需的类型定义(例如yarn add --dev @types/jsdom @types/node
)并导入以使用:
import { DOMWindow, JSDOM } from 'jsdom'
interface Global extends NodeJS.Global {
window: DOMWindow,
document: Document,
navigator: {
userAgent: string
}
}
const globalNode: Global = {
window: window,
document: window.document,
navigator: {
userAgent: 'node.js',
},
...global
}
Run Code Online (Sandbox Code Playgroud)
这是正确的解决方案,而不是使用 Typescript 的命名空间。它还兼容所有 eslint 默认规则:
// Declare a type.
interface CustomNodeJsGlobal extends NodeJS.Global {
myExtraGlobalVariable: number;
// You can declare anything you need.
}
Run Code Online (Sandbox Code Playgroud)
用它:
// Tell Typescript to use this type on the globally scoped `global` variable.
declare const global: CustomNodeJsGlobal;
function doSomething() {
// Use it freely
global.myExtraGlobalVariable = 5;
}
doSomething();
Run Code Online (Sandbox Code Playgroud)
global
global
声明文件,例如:src/types/index.d.ts
global
声明文件添加到tsconfig.json
:{\n "compilerOptions": {\n /* ... */\n "typeRoots": [\n "./src/types",\n ],\n /* ... */\n },\n /* ... */\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n\n您可以从模块内部将声明添加到全局范围
\n
因此,例如,如果您想向数组接口添加一些额外的内容,您需要执行以下操作:
\nobservable.ts
(例子)
{\n "compilerOptions": {\n /* ... */\n "typeRoots": [\n "./src/types",\n ],\n /* ... */\n },\n /* ... */\n}\n
Run Code Online (Sandbox Code Playgroud)\nsrc/types/index.d.ts
// observable.ts\nexport class Observable<T> {\n // ... still no implementation ...\n}\n
Run Code Online (Sandbox Code Playgroud)\nsomeWhereInApp.ts
declare global {\n interface Array<T> {\n toObservable(): Observable<T>;\n }\n}\n
Run Code Online (Sandbox Code Playgroud)\nglobal
如果你想添加一个全局变量来用作global.myVariable
您需要执行的操作:
src/types/index.d.ts
Array.prototype.toObservable = function () {\n // ...\n};\n
Run Code Online (Sandbox Code Playgroud)\nsomeWhereInApp.ts
/* eslint-disable no-var */\ndeclare global {\n/* \xe2\x86\x93\xe2\x86\x93\xe2\x86\x93 "var" is important */ \n var myVariable: string[];\n}\n/* eslint-enable no-var */\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
20139 次 |
最近记录: |