Javascript 我如何修复错误;元素隐式地具有一个';任何';类型,因为类型的表达式
我是打字的初学者 我使用Javascript 我如何修复错误;元素隐式地具有一个';任何';类型,因为类型的表达式,javascript,reactjs,typescript,Javascript,Reactjs,Typescript,我是打字的初学者 我使用ts migrate在我的react应用程序中用TypeScript替换JavaScript,但是我得到了以下错误 元素隐式具有“any”类型,因为“any”类型的表达式不能用于索引类型“{}”。TS7053 导致错误的文件如下 import React,{useState,useffect,useCallback}来自'React'; 导入“./assets/styles/style.css”; 从“/components/index”导入{AnswersList,Ch
ts migrate
在我的react应用程序中用TypeScript替换JavaScript,但是我得到了以下错误
元素隐式具有“any”类型,因为“any”类型的表达式不能用于索引类型“{}”。TS7053
导致错误的文件如下
import React,{useState,useffect,useCallback}来自'React';
导入“./assets/styles/style.css”;
从“/components/index”导入{AnswersList,Chats};
从“./components/Forms/FormDialog”导入FormDialog;
从“./firebase/index”导入{db};
常量应用=()=>{
const[answers,setAnswers]=useState([]);
const[chats,setChats]=useState([]);
const[currentId,setCurrentId]=useState(“init”);
const[dataset,setDataset]=useState({});
const[open,setOpen]=useState(false);
const displayNextQuestion=(nextQuestionId:any,nextDataset:any)=>{
addChats({
text:nextDataset.question,
键入:“问题”
})
setAnswers(nextDataset.answers)
setCurrentId(nextQuestionId)
}
const selectAnswer=(selectedAnswer:any,nextQuestionId:any)=>{
开关(真){
案例(nextQuestionId=='contact'):
handleClickOpen()
打破
案例(/^https:/.test(nextQuestionId)):
常量a=document.createElement('a');
a、 href=nextQuestionId;
a、 目标='u blank';
a、 单击();
打破
违约:
addChats({
文本:选择回答,
键入:“答案”
})
setTimeout(()=>displayNextQuestion(nextQuestionId,dataset[nextQuestionId]),500)//这里是发生错误的地方
打破
}
}
const addChats=(chat:any)=>{
setChats(prevChats=>{
return[…prevChats,chat]
})
}
常量handleClickOpen=()=>{
setOpen(真)
};
const handleClose=useCallback(()=>{
setOpen(假)
},[setOpen]);
useffect(()=>{
(异步()=>{
const initDataset={};
等待db.collection('questions').get()。然后(快照=>{
snapshots.forEach(doc=>{
const id=doc.id
常量数据=文档数据()
initDataset[id]=数据
})
})
setDataset(initDataset)
displayNextQuestion(currentId,initDataset[currentId])
})()
}, [])
useffect(()=>{
const scrollArea=document.getElementById('scroll-area'))
如果(滚动区域){
scrollArea.scrollTop=scrollArea.scrollHeight
}
})
返回(
);
}
导出默认应用程序;
首先,我使用如下界面定义了displayNextQuestion
的类型,但是错误没有解决,我们得到了另一个错误
interface StringKeyObject {
[key: string]: any;
}
const displayNextQuestion: StringKeyObject = (nextQuestionId: any, nextDataset: any) => {
addChats({
text: nextDataset.question,
type: 'question'
})
setAnswers(nextDataset.answers)
setCurrentId(nextQuestionId)
}
此表达式不可调用。类型“StringKeyObject”没有调用签名。TS2349
接下来,displaytnextquestion
的参数nextQuestionId
的类型为any,因此我将其设置为string,但错误没有改变
我做了很多研究,不知道如何解决这个错误,所以我问。我不确定,因为没有提到错误发生的确切位置 也许我认为你应该为
useState
使用泛型
你能检查一下下面的工作情况吗
类型数据集={
//编写要管理的状态的形式。
}
const[dataset,setDataset]=useState({});
当你说:
const displayNextQuestion: StringKeyObject = (...)
这意味着displayNextQuestion
属于StringKeyObject
类型,但显然不是。这是一个函数。您也不会从此函数返回任何内容,因此返回类型也是void
现在让我们看看参数nextQuestionId
必须是字符串
,因为您使用setCurrentId(nextQuestionId)
,其默认状态为init
nextDataset
应该是一个对象,该对象具有如下答案数组:
{
answers: [...],
...
}
把所有这些放在一起,像这样的东西应该会起作用:
const displayNextQuestion = (nextQuestionId: string, nextDataset: { answers: Array<Answer> } ) => {
addChats({
text: nextDataset.question,
type: 'question'
})
setAnswers(nextDataset.answers)
setCurrentId(nextQuestionId)
}
并显式设置useState
的类型:
interface Question {}
interface Chat {}
interface Answer { }
interface Dataset {
answers: Array<Answer>;
question: any;
[k: string]: Question;
// ^ This signifies that Dataset can have any key as long as it's string
// and its value is of type Question (best I could guess based on the
// usage at "dataset[nextQuestionId])"
}
const App = () => {
const [answers, setAnswers] = useState<Array<Answer>>([]);
const [chats, setChats] = useState<Array<Chat>>([]);
const [currentId, setCurrentId] = useState("init");
const [dataset, setDataset] = useState<Dataset>({ answers: [], question: null });
const [open, setOpen] = useState(false);
// ...
接口问题{}
界面聊天{}
接口应答{}
接口数据集{
答:数组;
问题:有否,;
[k:字符串]:问题;
//^这表示数据集可以有任何键,只要它是字符串
//它的值是问题类型(根据
//“数据集[nextQuestionId])的用法
}
常量应用=()=>{
const[answers,setAnswers]=useState([]);
const[chats,setChats]=useState([]);
const[currentId,setCurrentId]=useState(“init”);
const[dataset,setDataset]=useState({答案:[],问题:null});
const[open,setOpen]=useState(false);
// ...
interface Question {}
interface Chat {}
interface Answer { }
interface Dataset {
answers: Array<Answer>;
question: any;
[k: string]: Question;
// ^ This signifies that Dataset can have any key as long as it's string
// and its value is of type Question (best I could guess based on the
// usage at "dataset[nextQuestionId])"
}
const App = () => {
const [answers, setAnswers] = useState<Array<Answer>>([]);
const [chats, setChats] = useState<Array<Chat>>([]);
const [currentId, setCurrentId] = useState("init");
const [dataset, setDataset] = useState<Dataset>({ answers: [], question: null });
const [open, setOpen] = useState(false);
// ...