[英]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
的类型是{}
但您正在尝试插入键storyFile
和quetzalData
在{}
类型中不存在的
To solve this, you first need to know the type of FyreVMData
.要解决这个问题,您首先需要知道FyreVMData
的类型。
FyreVMData
variable has predefined keys, then use following如果FyreVMData
变量具有预定义的键,则使用以下FyreVMData: {
storyFile: ArrayBuffer;
quetzalData: any; // or what is returned by this.wrapper.saveGame();
};
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.