首页 > 其他分享 >TypeScript 中 Type 'typeof globalThis' has no index signature 错误解决

TypeScript 中 Type 'typeof globalThis' has no index signature 错误解决

时间:2024-04-17 14:35:26浏览次数:20  
标签:index TypeScript no global ts number example 属性

TypeScript 中 Type 'typeof globalThis' has no index signature 错误解决

当我们尝试访问 global 对象上不存在的属性时,会出现错误“Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature”。 要解决此错误,需要扩展全局对象并为必要的属性指定类型。

下面代码是一个发生该错误的示例。

// Error: Element implicitly has an 'any'
// type because type 'typeof globalThis'
// has no index signature.ts(7017)
global.hello = 'world';

我们试图访问 global 对象上不存在的属性,因此会看到报错。

为了解决这个问题,我们必须为我们打算在 global 对象上访问的属性和方法添加类型。

在 src 目录中,创建一个包含以下 index.d.ts 文件的 types 目录:

src/types/index.d.ts

/* eslint-disable no-var */

declare global {
  var example: string;
  function sum(a: number, b: number): number;
}

export {};

我们添加了一个 example 属性,该属性具有字符串类型和 sum 方法。

注意 ,这在不同的用例中会有所不同,因此请确保调整属性名称和类型。

确保使用 var 关键字为你打算在其他文件中设置和使用的属性添加类型。

我们需要在全局对象上添加要访问的所有属性的名称和类型。

例如,如果不知道特定属性的类型并想关闭类型检查,需要将其设置为 any。

现在,我可以设置和访问 global 对象上的指定属性,而不会出现任何错误。

global.example = 'hello world';

global.sum = function (a: number, b: number) {
  return a + b;
};

console.log(global.example); // "hello world"

console.log(global.sum(15, 25)); // 40

注意,如果您使用的是 ts-node,那么终端可能仍会出现错误。

问题在于 ts-node 无法识别本地声明文件。

要解决这个问题,请在 ts-node 命令中使用 --files 标志,因此应该运行 ts-node --files ./src/index.ts 而不是 ts-node ./src/index.ts

将 nodemon 与 ts-node 一起使用,下面是我的 nodemon.json 文件的内容

{
  "watch": ["src"],
  "ext": ".ts,.js",
  "ignore": [],
  "exec": "ts-node --files ./src/index.ts"
}

跟上 --files 选项后(仅在使用 ts-node 时才需要),重新启动服务器,应该会一切顺利。

注意 ,这使得 sum 函数和 example 属性可以直接访问,也可以在 global 对象上访问。

global.example = 'hello world';

global.sum = function (a: number, b: number) {
  return a + b;
};

console.log(global.example); // "hello world"
console.log(global.sum(15, 25)); // 40

console.log(example); // "hello world"
console.log(sum(5, 15)); // 20

如果您的 IDE 中仍然出现错误,请尝试将 types 目录的路径添加到 tsconfig.json 文件中。

{
  "compilerOptions": {
    // ... rest
    "typeRoots": ["./node_modules/@types", "./src/types"]
  }
}

我们在 index.d.ts 文件中使用 export {} 将其标记为外部模块。 模块是包含至少 1 个导入或导出语句的文件。 我们必须这样做才能扩大 global 的范围。

注意 ,我们必须根据用例更改 index.d.ts 文件的内容。

你应该在全局对象上添加打算访问的所有属性的名称(和类型)。

src/types/index.d.ts

declare global {
  var example: any; // 禁用属性的类型检查
  function sum(a: number, b: number): number;
}

export {};

提供的文件只是添加了一个类型为 any 的 example 属性和一个 sum 方法,这很可能不是我们需要的。

TypeScript 在查找常规  .ts 文件的相同位置查找  .d.ts 文件,这取决于 tsconfig.json 文件中的 include 和 exclude 设置。

TypeScript 会将你在 global 对象上声明的类型与原始类型合并,因此你将能够从两个声明中访问属性和方法。

转载自:https://www.jiyik.com/tm/xwzj/web_928.html

标签:index,TypeScript,no,global,ts,number,example,属性
From: https://www.cnblogs.com/UnfetteredMan/p/18140655

相关文章

  • LeetCode 1315. Sum of Nodes with Even-Valued Grandparent
    原题链接在这里:https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/description/题目:Giventhe root ofabinarytree,return thesumofvaluesofnodeswithan even-valuedgrandparent.Iftherearenonodeswithan even-valuedgrandpar......
  • 解决.Net6 部署到ubuntu22.04中使用DotNetCore.NPOI 导出报 Could not open display (
    在Ubuntu22环境下,出现"Couldnotopendisplay(X-Serverrequired.CheckyourDISPLAYenvironmentvariable)"错误可能是由于缺少X服务器或未正确配置DISPLAY环境变量导致的。以下是你可以尝试的解决方法:检查DISPLAY环境变量:确保DISPLAY环境变量已正确设置。使......
  • 【git】ignore文件详解
    1.功能可以指定git,对于某些文件或者文件夹不进行追踪记录,例如一些依赖或者编译中间文件等。2.使用注意2.1对于已经有提交记录的文件对于已经提交的文件,即使加入了ignore文件也会被git追踪,想要不被追踪,需要先删除文件然后,提交再添加至ignore文件,即可生效(例如c项目一些编......
  • P2Streaming Node
    Overview  组成: 当Framework丢过来一个Frame时,会根据以上流程依次处理Frame,在StreamingFeaturePipe中RootNode总是作为第一个节点,最后一个节点一定是HelperNode,TPINode用于挂载三方算法的节点,P2Stream......
  • Idea报错 【cannot access com.xxx】的【解决办法】
    正常操作代码,一个类突然标红,提示 cannotaccesscom.xxx 。执行下面操作,执行完后项目变为正常 但是如果是你代码存在异常,可不是清缓存就能解决了,就要靠自己了哈哈 第一步File知道吧! 第二步点我箭头的位置知道吧 第三步点中间的ok?意思就是清缓存并重启idea的意......
  • 解决IDA Pro Warning:mssdk64_win7: No such file or directory
       最初我以为出现这个问题的原因是我本机没有安装Windows7sdk导致的,然后尝试安装了windows7sdk。发现并没有什么用。接着机缘巧合之下发现这里的mssdk64_win7并不是指windowssdk,而是指idapro使用的til文件。所以idapro报这个错误只是因为安装目录下缺失了这些文件,所以......
  • P2178 [NOI2015] 品酒大会 题解(评分:8.0)(2024.2.23)
    前言"I'mfree."做法与题解区都不同,虽然麻烦,但是毕竟复杂度是对的,而且想法很自然,还是写一写吧!Solution题意:给定长为\(n\)的字符串\(s\)和长为\(n\)的数组\(A\),对于每个\(r\),求满足\(\text{LCP}(\text{Suffix}(x),\text{Suffix}(y))\ger,x<y\)的数对\((x,y)\)数......
  • 控制台连接数据库成功,驱动也导入了没问题,但tomcat的servlet启动会报错。报错信息:java.
    查了很多资料这个报错信息是因为驱动导入不成功,但我确定我已经导入了,因为我在控制台输出了出来。找了很久,发现了这个小问题。原因是我在Eclipse中创建是一个标准的Web工程,它是自带lib文件夹的,然后我自己又自建了一个lib文件夹用来放驱动,结果就像这样报出一个错误!解决方案:......
  • Parcharm-ModuleNotFoundError: No module named 'request'--解决方案
    问题:在Pycharm中报requestsmodule找不到特别的地方:已经通过“pip3installrequests”的命令安装过requests这个模块,并能顺利运行,但是不能在Pycharm中运行 解决方案如下:1.找到Pycharm中的setting设置,并打开2.找到自己工作的目录下的“PythonInterpreter”-->"+......
  • Could not set parameters for mapping
    错误现象:Resolved[org.mybatis.spring.MyBatisSystemException:nestedexceptionisorg.apache.ibatis.type.TypeException:Couldnotsetparametersformapping:ParameterMapping{property='keywords',mode=IN,javaType=classjava.lang.Object,jdbcType=n......