简体   繁体   English

Typescript - 类型“字符串”不能用于索引类型

[英]Typescript - type 'string' can't be used to index type

I'm using Vue.js with TypeScript and have run into some errors when using dynamic object keys.我将 Vue.js 与 TypeScript 一起使用,并且在使用动态 object 密钥时遇到了一些错误。 Here is my function:这是我的 function:

  methods: {
    interface ResetStateItems {
      selected: WebspaceBackupArtifactExtended[];
      selectedDirectory: string;
      artifacts: WebspaceBackupArtifactExtended[];
      isLoadingDirectory: boolean;
      restoreComplete: boolean;
      isProcessingBackupRestore: boolean;
    }

    resetPage() {
      const resetStateItems: ResetStateItems = {
        selected: [],
        selectedDirectory: '/',
        artifacts: [],
        isLoadingDirectory: false,
        restoreComplete: false,
        isProcessingBackupRestore: false,
      };

      Object.keys(resetStateItems).forEach((stateItem: string) => {
        this[stateItem as keyof resetStateItems] = resetStateItems[stateItem];
      });
      this.getWebspaceBackupByWebspaceIdAndId();
    },

Here are the errors that I get:以下是我得到的错误:

ERROR in /app/src/views/BackupAndRestore/BackupAndRestoreWebspaceBackup.vue(484,9):
my-project    | 484:9 Type 'any' is not assignable to type 'never'.
my-project    |     482 |
my-project    |     483 |       Object.keys(resetStateItems).forEach((stateItem: string) => {
my-project    |   > 484 |         this[stateItem as keyof ResetStateItems] = resetStateItems[stateItem];
my-project    |         |         ^
my-project    |     485 |       });
my-project    |     486 |       this.getWebspaceBackupByWebspaceIdAndId();
my-project    |     487 |     },
ERROR in /app/src/views/BackupAndRestore/BackupAndRestoreWebspaceBackup.vue(484,52):
> my-project    | 484:52 Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'ResetStateItems'.
> my-project    |   No index signature with a parameter of type 'string' was found on type 'ResetStateItems'.
> my-project    |     482 |
> my-project    |     483 |       Object.keys(resetStateItems).forEach((stateItem: string) => {
> my-project    |   > 484 |         this[stateItem as keyof ResetStateItems] = resetStateItems[stateItem];
> my-project    |         |                                                    ^
> my-project    |     485 |       });
> my-project    |     486 |       this.getWebspaceBackupByWebspaceIdAndId();
> my-project    |     487 |     },

I'm not entirely sure what the issue is as I have defined the interface ResetStateItems and thought I could just use keyof to make it work?我不完全确定问题是什么,因为我已经定义了接口ResetStateItems并认为我可以使用keyof使其工作?

Instead of using the variable name with the keyof statement, use the interface name:不要在keyof语句中使用变量名,而是使用接口名:

this[stateItem as keyof ResetStateItems] = resetStateItems[stateItem];

暂无
暂无

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

相关问题 useRef typescript 'string' 不能用于索引类型'{}' - useRef typescript 'string' can't be used to index type '{}' TypeScript - 元素隐式具有“任意”类型,因为“字符串”类型的表达式不能用于索引类型 - 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 React Typescript TypeScript - ts(7053):元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引 - TypeScript - ts(7053) : Element implicitly has an 'any' type because expression of type 'string' can't be used to index 'string' 不能用于索引类型 '{}' - 'string' can't be used to index type '{}' TypeScript 错误:“元素隐式具有 'any' 类型,因为类型 'any' 的表达式不能用于索引类型 - TypeScript Err: "Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 打字稿错误-类型'string []'不能用作索引类型 - Typescript error - Type 'string[]' cannot be used as an index type 反应 typescript - 元素隐式具有“任何”类型,因为使用国家标志图标时,“字符串”类型的表达式不能用于索引类型 - react typescript - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type when using country-flag-icons Typescript 元素隐式具有“任何”类型,因为“数字”类型的表达式不能用于索引 - Typescript Element implicitly has an 'any' type because expression of type 'number' can't be used to index “字符串”类型的表达式不能用于索引“对象”类型。 在“对象”类型上找不到带有“字符串”类型参数的索引签名 - expression of type 'string' can't be used to index type 'Object'. No index signature with a parameter of type 'string' was found on type 'Object'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM