【问题标题】:How can i fix the Element Implicity and No index Signature Error?如何隐式修复元素且无索引签名错误?
【发布时间】:2022-01-15 20:46:09
【问题描述】:

我在specialCodes[letter] 中收到一个错误,说元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型没有索引签名在类型上找到了“字符串”类型的参数

import { Message, MessageEmbed } from "discord.js";
import BaseCommand from "../../utils/structures/BaseCommand";
import DiscordClient from "../../client/client";

export default class EmojifyCommand extends BaseCommand {
  constructor() {
    super("emojify", "fun", []);
    /**
     * @param {Client} client
     * @param {Message} message
     * @param {String[]} args
     */
  }

  async run(client: DiscordClient, message: Message, args: Array<string>) {
    const embedSpecify = new MessageEmbed();
    embedSpecify
      .setColor("RED")
      .setDescription("Please specify a text to translate!");

    if (!args.length) return message.reply({ embeds: [embedSpecify] });

    const specialCodes = {
      0: ":zero:",
      1: ":one:",
      2: ":two:",
      3: ":three:",
      4: ":four:",
      5: ":five:",
      6: ":six:",
      7: ":seven:",
      8: ":eight:",
      9: ":nine:",
      "#": ":hash:",
      "*": ":asterisk:",
      "?": ":grey_question:",
      "!": ":grey_exclamation:",
      " ": "   ",
    };

    const text = args
      .join(" ")
      .toLowerCase()
      .split("")
      .map((letter) => {
        if (/[a-z]/g.test(letter)) {
          return `:regional_indicator_${letter}:`;
        } else if (specialCodes[letter]) {
          return `${specialCodes[letter]}`;
        }
        return letter;
      })
      .join("");

    message.reply(text);
  }
}

【问题讨论】:

    标签: node.js typescript discord discord.js


    【解决方案1】:

    您在specialCodes 中有混合类型的键,您需要帮助 TS 了解如何从那里获取值。像这样的:

    const specialCodes: Record<number | string, string> = {
          0: ":zero:",
          1: ":one:",
          2: ":two:",
          3: ":three:",
          4: ":four:",
          5: ":five:",
          6: ":six:",
          7: ":seven:",
          8: ":eight:",
          9: ":nine:",
          "#": ":hash:",
          "*": ":asterisk:",
          "?": ":grey_question:",
          "!": ":grey_exclamation:",
          " ": "   ",
        };
    
    

    【讨论】:

      猜你喜欢
      • 2020-06-27
      • 2020-09-22
      • 2015-07-20
      • 2018-04-09
      • 2017-05-13
      • 1970-01-01
      • 2017-06-30
      • 2019-11-11
      • 2018-09-06
      相关资源
      最近更新 更多