typescript 中的声明或语句预期错误
本篇文章解释了 javascript 或 typescript 中的 declaration or statement expected
错误以及编译器抛出此错误的原因。将讨论此错误的所有主要原因,以及如何在开发人员社区中避免它。
javascript 或 typescript 中的预期的声明或语句
错误
当我们在代码中出现语法错误时,就会出现 javascript 或 typescript 中的 declaration or statement expected
错误。
例如,考虑使用错误的语法对文件中的对象进行解构,使用错误的名称导出文件中的模块,或者括号丢失或不一致。
考虑以下代码示例,其中由于代码中的语法错误而出现不同的 declaration or statement expected
。
let onedata: number;
const obj = {
val: 1,
};
// 1. ⛔️ parsing error: declaration or statement expected.
{ onedata } = obj; // 👈️ this must be wrapped in parenthesis
const sumobj = (a: number, b: number) => a b;
// 2. ⛔️ error: parsing error: declaration or statement expected.eslint
export sumobj // 👈️ should be export {sum}
// 3. make sure you're not using reserved words
const caseval = 'hello world' // 👈️ case is reserved word
上面的代码产生以下错误,写在下面。
//output or errors
variable 'one' is used before being assigned.
declaration or statement expected. this '=' follows a block of statements, so if you intend to write a destructuring assignment, you might need to wrap the whole assignment in parentheses.
declaration or statement expected.
'case' is not allowed as a variable declaration name.
variable declaration expected.
variable declaration expected.
考虑以下代码,正确编译且没有 declaration or statement expected
错误。
let val: number;
const obj = {
val: 1,
};
// ✅ ok
({ val } = obj); // 👈️ this must be wrapped in parenthesis
console.log(val); // 👉️ 1
上面的代码产生以下输出。
1
在导出之前已声明的内容时,有时也会出现预期声明或声明
错误。每当需要执行此操作时,将导出用花括号括起来。
const objsum = (a: number, b: number) => a b;
// ✅ ok
export { objsum };
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
在 typescript 中使用 try..catch..finally 处理异常
发布时间:2023/03/19 浏览次数:385 分类:
-
本文详细介绍了如何在 typescript 中使用 try..catch..finally 进行异常处理,并附有示例。
发布时间:2023/03/19 浏览次数:254 分类:
-
本教程指南通过特定的实现和编码示例深入了解了 typescript 中 declare 关键字的用途。
发布时间:2023/03/19 浏览次数:962 分类:
-
本篇文章演示了类的 get 和 set 属性以及如何在 typescript 中实现它。
在 typescript 中格式化日期和时间
发布时间:2023/03/19 浏览次数:269 分类:
-
本教程介绍内置对象 date() 并讨论在 typescript 中获取、设置和格式化日期和时间的各种方法。
在 typescript 中返回一个 promise
发布时间:2023/03/19 浏览次数:586 分类:
-
本教程讨论如何在 typescript 中返回正确的 promise。这将提供 typescript 中 returns promise 的完整编码示例,并完整演示每个步骤。
在 typescript 中定义函数回调的类型
发布时间:2023/03/19 浏览次数:1445 分类:
-
本教程说明了在 typescript 中为函数回调定义类型的ag捕鱼王app官网的解决方案。为了程序员的方便和方便,实施了不同的编码实践指南。
使用 npm 将 typescript 更新到最新版本
发布时间:2023/03/19 浏览次数:446 分类:
-
本教程说明了如何使用 npm 更新到最新版本的 typescript。这将为如何使用 npm 将 typescript 更新到最新版本提供完整的实际示例。