收到错误消息:类型为{JSON对象}的参数无法分配给“never”的参数。

huangapple go评论69阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年7月23日 15:03:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747000.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定