简体   繁体   English

TypeScript 在 object 中查找密钥的问题

[英]TypeScript issue with finding key in object

I'm trying to learn typescript as I build a reactjs app, and it seems like I can't help tripping over TS errors.当我构建一个 reactjs 应用程序时,我正在尝试学习 typescript,似乎我不禁被 TS 错误绊倒。 I have built a lookup function ( helloMap ) to translate one value into another.我建立了一个查找 function ( helloMap ) 来将一个值转换为另一个值。 Example here: https://codesandbox.io/s/withered-worker-yb66l?file=/src/App.tsx此处示例: https://codesandbox.io/s/withered-worker-yb66l?file=/src/App.tsx

It seems very simple and straightforward, and the sample actually works in codesandbox, but it shows a TS error of (parameter) greeting: string Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ Hi: string; "Good day": string; Greets: string; }'. No index signature with a parameter of type 'string' was found on type '{ Hi: string; "Good day": string; Greets: string; }'.ts(7053)看起来非常简单直接,并且示例实际上在codesandbox中工作,但它显示了(parameter) greeting: string Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ Hi: string; "Good day": string; Greets: string; }'. No index signature with a parameter of type 'string' was found on type '{ Hi: string; "Good day": string; Greets: string; }'.ts(7053) (parameter) greeting: string Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ Hi: string; "Good day": string; Greets: string; }'. No index signature with a parameter of type 'string' was found on type '{ Hi: string; "Good day": string; Greets: string; }'.ts(7053)

import * as React from "react";
import "./styles.css";

export default function App() {
  const helloMap = (greeting: string) => {
    let hello_map = {
      Hi: "Hola",
      "Good day": "Whattup",
      Greets: "Hello"
    };

    return hello_map[greeting]; // error here
  };

  return (
    <div className="App">
      <h1>{helloMap("Good day")} CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
    </div>
  );
}

In my local app this error causes the display to fail to render, and though codesandbox appears to be running a little less strictly it still shows the error in the IDE.在我的本地应用程序中,此错误导致显示无法呈现,尽管代码和框的运行似乎不太严格,但它仍然在 IDE 中显示错误。

its because of you didn't provided type for hello_map这是因为您没有为 hello_map 提供类型

Should be { [key: string]: string }应该是 { [key: string]: string }

import * as React from "react";
import "./styles.css";

export default function App() {
  const helloMap = (greeting: string) => {
    let hello_map: { [key: string]: string } = {
      Hi: "Hola",
      "Good day": "Whattup",
      Greets: "Hello"
    };

    return hello_map[greeting];
  };

  return (
    <div className="App">
      <h1>{helloMap("Good day")} CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
    </div>
  );
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM