类型“{ title: string; time: string; content: { id: number; content: string; }[]; }”的参数不能赋给类型“never”的参数。ts
时间: 2023-09-23 20:03:36 浏览: 50
这个错误通常是由于在函数或方法返回类型声明为 `never`,但实际上返回了一个非 `never` 类型的值,导致类型不匹配。需要检查一下函数或方法中的返回语句,确保返回的类型与函数或方法的返回类型一致。
比如,下面的代码中,函数返回类型声明为 `never`,但函数中却返回了一个对象:
```typescript
function getNews(): never {
return {
title: 'news title',
time: '2021-08-01',
content: [
{ id: 1, content: 'news content 1' },
{ id: 2, content: 'news content 2' }
]
}
}
```
此时,可以将函数返回类型改为与返回值类型一致,即 `{ title: string, time: string, content: { id: number, content: string }[] }`:
```typescript
function getNews(): { title: string, time: string, content: { id: number, content: string }[] } {
return {
title: 'news title',
time: '2021-08-01',
content: [
{ id: 1, content: 'news content 1' },
{ id: 2, content: 'news content 2' }
]
}
}
```
这样就能避免类型不匹配的错误了。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)