react + ts使用问题集合

993 阅读1分钟

一、模块“XXX”只能在使用“esModuleInterop”标志时进行默认导入

image.png 使用如下方式引用

import * as React from 'react'

二、不能将类型“{ children: (string | Element)[]; name: string; }”分配给类型“IntrinsicAttributes & IProps”。

报错信息:类型“IntrinsicAttributes & IProps”上不存在属性“children”。 Type '{ children: (string | Element)[]; name: string; }' is not assignable to type 'IntrinsicAttributes & IProps'. Property 'children' does not exist on type 'IntrinsicAttributes & IProps'.

image.png

解决方式1:在child1组件中将child定义为node节点

image.png

解决方式2:使用React.PropsWithChildren

const Child1: React.FC<React.PropsWithChildren<IProps>> = (props) => {}

三、Cannot set properties of null (setting 'innerText'

在改变用useRef定义的变量值时报错