英文:
Getting the error: Argument of type {JSON Object} is not assignable to the parameter of 'never'
问题
我遇到了问题,正在尝试使用 TypeScript、axios 和 cheerio 进行网页抓取,但出现了以下错误:
> 参数类型 '{ contestcode: string; contestname: string; conteststart: string; contestduration: string; startsin: string; }' 不能赋给类型 'never' 的参数。
这是我的代码:
// 用于数据抓取的函数
async function datascraper() {
// 尝试捕获异常
try {
// 通过 axios 获取 URL
const { data } = await axios.get(url);
// 使用 cheerio 加载 HTML
const $ = load(data);
// 定义目标类
const contests = $('.MuiTableBody-root', $.html());
// 用于存放 JSON 对象的数组
const contestlist = [];
contests.each((idx, el) => {
// 创建对象
const contestdetails = {
contestcode: '',
contestname: '',
conteststart: '',
contestduration: '',
startsin: '',
};
contestdetails.contestcode = $(el).children('p').text();
// 错误:参数类型 '{ contestcode: string; contestname: string; conteststart: string; contestduration: string; startsin: string; }' 不能赋给类型 'never' 的参数。
contestlist.push(contestdetails);
});
console.dir(contestlist);
} catch (err) {
console.log(err);
}
}
我在contestlist.push(contestdetails)
行遇到了错误。
我尝试通过创建一个接口并将数组定义为 string[]
来进行故障排除,但仍然不起作用,我已经没有更多的想法了。我一直得到相同的错误。
英文:
I'm trying to do some web scraping using typescript, axios and cheerio but I'm running into issues with this error:
>Argument of type '{ contestcode: string; contestname: string; conteststart: string; contestduration: string; startsin: string; }' is not assignable to parameter of type 'never'.
this is my code
//function for data scraping
async function datascraper(){
//try catch block
try{
//getting the url from axios
const {data} = await axios.get(url);
//loading the html using cheerio load
const $ = load(data);
//defining the target class
const contests = $('.MuiTableBody-root',$.html());
//array to fit in the json objects
const contestlist = [];
contests.each((idx,el)=>{
//creating an object
const contestdetails = {contestcode: '',
contestname: '',
conteststart: '',
contestduration: '',
startsin:''}
contestdetails.contestcode = $(el).children('p').text();
//error: Argument of type '{ contestcode: string; contestname: string; conteststart: string; contestduration: string; startsin: string; }' is not assignable to parameter of type 'never'.
contestlist.push(contestdetails);
});
console.dir(contestlist);
}
catch(err){
console.log(err);
}
}
I'm getting the error at contestlist.push(contestdetails)
line
I've tried trouble shooting it by creating an interface and defining the array as a string[] but nothing is working and I ran out of ideas. I keep getting the same error.
答案1
得分: 0
当您实例化一个数组时,如果没有显式为其指定类型(且数组本身不包含任何类型信息),TypeScript 会默认将该数组类型设定为 never[]
。
const contestlist = ['someString']; // string[]
const contestlist = [123]; // number[]
const contestlist = []; // never[]
如果您打算稍后向该数组推送特定类型的值,那么显式为数组指定类型将避免这种类型推断路径。
type ContestDetails = {
contestcode: string;
contestname: string;
conteststart: string;
contestduration: string;
startsin: string;
}
const contestlist: ContestDetails[] = [] // ContestDetails[]
const contestdetails: ContestDetails = {
contestcode: '',
contestname: '',
conteststart: '',
contestduration: '',
startsin: ''
}
contestList.push(contestdetails) // 有效
英文:
When you instantiate an array without explicitly typing it (and the array itself doesn't contain any type information), TypeScript defaults to typing that array as never[]
const contestlist = ['someString']; // string[]
const contestlist = [123]; // number[]
const contestlist = []; // never[]
If you intend to push a value of a given type to this array later on, explicitly typing the array will avoid this inference path
type ContestDetails = {
contestcode: string;
contestname: string;
conteststart: string;
contestduration: string;
startsin: string;
}
const contestlist: ContestDetails[] = [] // ContestDetails[]
const contestdetails: ContestDetails = {
contestcode: '',
contestname: '',
conteststart: '',
contestduration: '',
startsin: ''
}
contestList.push(contestdetails) //valid
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论