React和Typescript-错误的类型检查

托马斯

我正在使用React + TypeScript编写我的第一个应用程序。我对为什么我的Visual Studio代码与CodeSandbox中的相同代码显示完全不同的类型检查感到非常困惑。

这是我的VSC显示的内容:[props.direction as any]

在此处输入图片说明

这是CodeSandbox中的corect方式:[props.direcion as FlexProps]。从我获得此示例的地方链接到Codesanbox:https ://codesandbox.io/s/funny-frost-1bxwk?file=/src/Heading.tsx

在此处输入图片说明

当我以这种方式重写代码时,一切正常:

export const Flex = styled.div`
display: flex;
flex-direction: ${(props : FlexProps) => props.direction}
`

但是,如果接口FlexProps具有更多类型,我将不得不在每行上分别重复它们。所以对我来说不是一个好方法。

我的package.json:

{
  "name": "interactive-visited-countries-map-react",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@material-ui/core": "^4.11.0",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "@types/jest": "^24.0.0",
    "@types/node": "^12.0.0",
    "@types/react": "^16.9.0",
    "@types/react-dom": "^16.9.0",
    "@types/styled-components": "^5.1.2",
    "@typescript-eslint/eslint-plugin": "^4.1.1",
    "@typescript-eslint/parser": "^4.1.1",
    "eslint": "^7.9.0",
    "eslint-plugin-react-hooks": "^4.0.8",
    "lodash.sortby": "^4.7.0",
    "node-sass": "^4.14.1",
    "normalize.css": "^8.0.1",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-scripts": "3.4.1",
    "react-tooltip": "^4.2.7",
    "styled-components": "^5.1.1",
    "styled-icons": "^10.19.0",
    "svg-url-loader": "^6.0.0",
    "typescript": "^4.0.2",
    "typescript-styled-plugin": "^0.15.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@types/lodash.sortby": "^4.7.6",
    "eslint-config-prettier": "^6.11.0",
    "eslint-plugin-prettier": "^3.1.4",
    "prettier": "^2.1.1"
  }
}

和tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "es2015", "es2017", "dom.iterable"],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": false,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react",
    "downlevelIteration": true,
    "declaration": true,
    "noImplicitAny": false,
    "plugins": [
      {
        "name": "typescript-styled-plugin"
      }
    ]
  },
  "include": ["src", "**/*.ts"]
}

托马斯

解:

我已经解决了一个问题。react-app-env.d我有

<reference types="react-scripts" /> 
declare module "styled-components"; 

我删除了declare module "styled-components",看来工作正常

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

React Redux Typescript:连接缺少类型错误

使用React Intl和Typescript检查消息是否为空

TypeScript和react-native的ForwardRef错误

React / TypeScript错误:运算符“ <”不能应用于类型“ boolean”和“ RegExp”

TypeScript高级类型检查

“ copyTo”处的断言失败错误。检查的图像尺寸和类型

React,Typescript和setState({[name]:value})错误

TypeScript React SFC子道具类型错误

TypeScript和React Native:RN样式的类型定义是否错误?

React和TypeScript中的HOC输入错误

React和Typescript组件对`Component`的道具类型

检查React组件的类型

与TypeScript和React的联合类型

React PropTypes检查错误的类型

TypeScript和React中的联合类型道具

React Hooks TypeScript事件和状态类型

React.ForwardRef TypeScript组件类型错误

TypeScript文件类型检查

如何在Typescript类型检查期间引发类型错误

如何使用react和typescript修复boolean或undefined类型的错误参数?

由于弱类型检查和缺少用于运行时类型检查的代码生成,导致打字稿结果错误

TypeScript 三元运算符隐藏类型错误 - 如何重写以使用类型检查?

样式组件和 Typescript 类型错误

React 和 Typescript,Axios 响应的类型?

如何修复错误类型 Item[] | undefined 不能使用 react 和 typescript 分配给 Item[] 类型?

简单样式组件和 Typescript 设置类型检查错误

typescript,检查对象类型,typeof得到编译错误

React Typescript中的按钮事件类型错误

React 和 Typescript,出现“预期类型”错误