将 Typescript 全局变量声明为“模块”类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30219843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:00:23  来源:igfitidea点击:

Declare Typescript global variable as "module" type

typescripttypescript1.5

提问by David Faivre

I have the React type definition file (that is declared using an external module). In my source files, I usually do:

我有 React 类型定义文件(使用外部模块声明)。在我的源文件中,我通常这样做:

import * as R from "react"

and can then happily use R.createElement(...etc in a strongly typed fashion.

然后可以愉快地R.createElement(...以强类型方式使用etc。

What I want is to not have to import Rin every file, and instead have it as a global declaration (yes, I'm willing to polute the global namespace with a few variables). I've tried:

我想要的是不必R在每个文件中都导入,而是将其作为全局声明(是的,我愿意用一些变量污染全局命名空间)。我试过了:

import * as React from "react";
declare var R : React;

This doesn't work though, I get "Cannot find name 'React'". Is there another way to export the entire module as global?

但这不起作用,我明白了"Cannot find name 'React'"。还有另一种方法可以将整个模块导出为全局吗?



Edit 1- I should have made clearer: I'm interested in how to export a global type definitionin a .d.tsfile. So assume I've attached Rto windowalready. Now I need typescript to know that Ris of type React module.

编辑 1- 我应该更清楚:我对如何在文件中导出全局类型定义感兴趣.d.ts。因此,假设我已经把它贴Rwindow了。现在我需要打字稿才能知道它R的类型React module

采纳答案by basarat

instead have it as a global

而是将其作为全局

There are two sides to this: global type declarationfor typescript and global variable availabilityfor JavaScript consumption.

这有两个方面:global type declaration用于打字稿和global variable availability用于 JavaScript 消费。

Global Type Declaration

全局类型声明

A .d.tsor a declaration only contributes to the globalname declaration space if there is no root level importor exportin the file. So have a file globalreact.d.tswhich will be an editedversion of https://github.com/borisyankov/DefinitelyTyped/blob/master/react/react.d.tsof the form declare module R(instead of declare module "react").

如果文件中没有根级导入导出,则a.d.ts或 a 声明仅对全局名称声明空间有贡献。所以有一个文件,它将是表单的https://github.com/borisyankov/DefinitelyTyped/blob/master/react/react.d.ts编辑版本(而不是)。globalreact.d.tsdeclare module Rdeclare module "react"

Global Variable Export

全局变量导出

You need to put it on windowin case of the browser. So do the following in a file makeReactGlobal.ts:

你需要把它放在window浏览器的情况下。所以在文件中执行以下操作makeReactGlobal.ts

var R = require('react'); 
(<any>window).R = R

And then in your application mainhave this file a dependency to ensure that it executes before any of your other code.

然后在您的应用程序中main有一个依赖项,以确保它在您的任何其他代码之前执行。

回答by David Faivre

Like @basarat said, it doesn't look like it's possible. @ahejlsberg himself weighed in on the issue on github: https://github.com/Microsoft/TypeScript/issues/3180#issuecomment-102523512.

就像@basarat 所说的,这看起来不可能。@ahejlsberg 本人在 github 上对这个问题进行了权衡:https: //github.com/Microsoft/TypeScript/issues/3180#issuecomment-102523512

回答by Remo H. Jansen

The declarekeyword does not declare a variable in the global scope. This keyword is used for cases in which there will be a variable in the global scope and you want to use it in a TypeScript without getting compilation errors.

declare关键字未在全局范围内声明变量。此关键字用于在全局范围内有一个变量并且您希望在 TypeScript 中使用它而不会出现编译错误的情况。

You can declare a global variable with:

您可以使用以下命令声明全局变量:

import * as R from "react";
window.R = R;