在我的 React-TypeScript-Electron 项目中,我有:
"devDependencies": {
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0"
},
"dependencies": {
"react": "^17.0.1",
"react-dom": "^17.0.1"
}
这是我的 tsconfig.json 文件:
{
"compilerOptions": {
"module": "CommonJS",
"moduleResolution": "node",
"lib": [
"dom",
"es2015",
"es2016",
"es2017",
"dom",
"dom.iterable",
],
"noImplicitAny": false,
"sourceMap": true,
"rootDir": "src",
"outDir": "dist",
"baseUrl": ".",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"jsx": "react",
"lib": ["dom", "esnext"],
"noEmitOnError": true,
"noImplicitReturns": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"removeComments": true,
"strict": true,
"downlevelIteration": true,
"declaration": true,
"alwaysStrict": true,
"skipLibCheck": true
},
"include": [
"src/**/*"
]
}
我收到这些错误消息:
对于此代码:
import React from 'react';
type Props = React.HTMLProps<HTMLDivElement, HTMLDivElement>
我收到此错误:
error TS2694: Namespace '"react"' has no exported member 'HTMLProps'.
type Props = React.HTMLProps<HTMLDivElement, HTMLDivElement>
对于此代码:
import React, { useState } from 'react';
interface Props {
summary: React.ReactNode;
children: () => React.ReactNode;
}
我收到这些错误:
error TS2694: Namespace '"react"' has no exported member 'ReactNode'
summary: React.ReactNode;
error TS2694: Namespace '"react"' has no exported member 'ReactNode'
children: () => React.ReactNode;
对于此代码:
import React from 'react'
type Value = React.ReactElement
我收到此错误:
error TS2694: Namespace '"react"' has no exported member
'ReactElement'
对于此代码:
import React, { useState } from 'react';
const onToggle = (e: React.SyntheticEvent) => {
我收到此错误:
error TS2694: Namespace '"react"' has no exported member
'SyntheticEvent'
对于此代码:
import React from 'react';
type Props =
React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>,
HTMLDivElement>
我收到这些错误:
error TS2694: Namespace '"react"' has no exported member
'DetailedHTMLProps'
对于此代码:
import React, { Fragment } from 'react';
export default function LogLink({ v, children }: Props) {
function onClick(e: React.SyntheticEvent) {
我收到此错误:
error TS2694: Namespace '"react"' has no exported member
'SyntheticEvent'
期待您的帮助。
感谢 Typescript Discord Group 中的人们,我发现在modules.d.ts 中声明react 和react-dom 会干扰类型定义。 删除 module.d.ts 中的这些定义后,所有这些错误都消失了
只要这样做,如果您的 Eslint 可以处理,就可以继续。 // any: 意味着孩子可以有任何类型
{
children: any
}