简体   繁体   English

TypeScript - 元素隐式具有“any”类型,因为“storyFile”类型的表达式不能用于索引类型“{}”

[英]TypeScript - Element implicitly has an 'any' type because expression of type '“storyFile”' can't be used to index type '{}'

I'm trying to upgrade an old React Typescript project and still running into problems.我正在尝试升级旧的 React Typescript 项目,但仍然遇到问题。

I resolved not having a constructor with the !我解决了没有带有 ! after each class level variable.在每个类级别变量之后。 Now it's complaining about the storyFile parameter.现在它在抱怨 storyFile 参数。 This is from the GitHub repo fyrevm-web.这是来自 GitHub 存储库 fyrevm-web。 For the exact file see: https://github.com/ChicagoDave/fyrevm-web/blob/master/src/FyreVMWeb/FyreVMMem.ts有关确切文件,请参阅: https : //github.com/ChicagoDave/fyrevm-web/blob/master/src/FyreVMWeb/FyreVMMem.ts

Any clues?有什么线索吗?

Element implicitly has an 'any' type because expression of type '"storyFile"' can't be used to index type '{}'.
  Property 'storyFile' does not exist on type '{}'.  TS7053

    25 |             this.wrapper = FyreVM.EngineWrapper.loadFromArrayBuffer(storyFile, true);
    26 |             this.ProcessCommand(this.wrapper.run());
  > 27 |             this.FyreVMData['storyFile'] = storyFile;
       |             ^
    28 |             this.FyreVMData['quetzalData'] = this.wrapper.saveGame();
    29 |             return this.FyreVMData;
    30 |         }

You need to declare the properties you're trying to assign on the FyreVMData type.您需要声明您尝试在FyreVMData类型上分配的FyreVMData

FyreVMData: {
  storyFile: ArrayBuffer;
  quetzalData: any; // or whatever the type `this.wrapper.saveGame()` returns
};

Read the error message carefully仔细阅读错误信息

Element implicitly has an 'any' type because expression of type '"storyFile"' can't be used to index type '{}'.
  Property 'storyFile' does not exist on type '{}'.  TS7053

Now, let's have a closer look at your code:现在,让我们仔细看看你的代码:

 // the type of this variable is {}
 FyreVMData: {};

 // ...after some lines, you have
 this.FyreVMData['storyFile'] = storyFile;
 this.FyreVMData['quetzalData'] = this.wrapper.saveGame();
  

The type of the FyreVMData is {} but you are trying to insert keys storyFile and quetzalData which are not there in type {} FyreVMData的类型是{}但您正在尝试插入键storyFilequetzalData{}类型中不存在的

To solve this, you first need to know the type of FyreVMData .要解决这个问题,您首先需要知道FyreVMData的类型。

  1. If the FyreVMData variable has predefined keys, then use following如果FyreVMData变量具有预定义的键,则使用以下
FyreVMData: {
  storyFile: ArrayBuffer;
  quetzalData: any; // or what is returned by this.wrapper.saveGame();
};
  1. If you want to insert dynamic keys into your FyreVMData object, then you need to define your object as follows如果你想在你的FyreVMData对象中插入动态键,那么你需要如下定义你的对象
// the key of FyreVMData object will be string, and value will be any
// you can modify the type of key/value based on your requirement.
FyreVMData: { [key: string]: any }

// now we can add dynamic keys to our FyreVMData object as follows:

FyreVMData["name"] = "stackoverflow"
FyreVMData["value"] = {}

If you want to allow implicit any you can update your tsconfig like this如果你想允许隐式任何你可以像这样更新你的 tsconfig

noImplicitAny: false

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeScript 错误:“元素隐式具有 'any' 类型,因为类型 'any' 的表达式不能用于索引类型 - TypeScript Err: "Element implicitly has an 'any' type because expression of type 'any' can't be used to index type TypeScript 错误元素隐式具有“任何”类型,因为“任何”类型的表达式不能用于索引类型 - TypeScript error Element implicitly has an 'any' type because expression of type 'any' can't be used to index type React typescript - 元素隐式具有“any”类型,因为“string”类型的表达式不能用于索引类型 - React typescript - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于在 React Typescript 中索引类型 - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type in React Typescript 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 React Typescript - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type React Typescript 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 - TypeScript 错误 - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type - TypeScript Error Typescript 错误:元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引 - Typescript error : Element implicitly has an 'any' type because expression of type 'string' can't be used to index 带有 React > Element 的 Typescript 隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引 - Typescript with React > Element implicitly has an 'any' type because expression of type 'string' can't be used to index 元素隐式具有 'any' 类型,因为类型 'any' 的表达式不能用于索引类型 '{}' - React Anagram - Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{}' - React Anagram map() 中的打字稿错误:元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 - Typescript error in map(): Element implicitly has an 'any' type because expression of type 'string' can't be used to index type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM