TypeScript Errors All In One
2024-09-05 09:55:14
TypeScript Errors All In One
1. Property 'name' has no initializer and is not definitely assigned in the constructor.ts
属性"名称"没有初始化程序,并且在构造函数中未明确分配
Strict Class Initialization
--strictPropertyInitialization
class C {
foo: number;
bar = "hello";
baz: boolean;
// Property 'baz' has no initializer and is not definitely assigned in the constructor.ts(2564)
constructor() {
this.foo = 42;
}
}
solutions
class CC {
foo: number;
bar = "hello";
baz: boolean | undefined;
constructor() {
this.foo = 42;
}
}
- tsconfig.json 关闭 Strict Class Initialization
tsconfig.json
{
"include": ["src/**/*"],
"exclude": ["tests/**/*"],
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"jsx": "preserve",
"declaration": false,
"declarationMap": false,
"sourceMap": true,
"outDir": "./build",
"rootDir": "./",
"removeComments": true,
"downlevelIteration": true,
"isolatedModules": false,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": false,// 关闭
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"esModuleInterop": true,
"allowUmdGlobalAccess": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
2. declaration for the 'Promise' constructor or include 'ES2015' in your --lib
option
interface Promise Represents the completion of an asynchronous operation
An async function or method in ES5/ES3 requires the 'Promise' constructor.
Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your --lib
option.ts(2705)
const getRequestTo = async <R>(endpoint: string): Promise<ApiResponseInterface<R>> => {
const request = await fetch(process.env.HOST + endpoint);
const response = await request.json();
return response;
};
const userResponse = getRequestTo('/user-data');
solution, How to fixed TypeScript Promise Error
just need to add a line of code
"lib": ["es2015"],
in yourtsconfig.json
{
// ...
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": ["es2015"],//
// ...
}
}
Cannot find name 'fetch'.ts(2304)
solution, How to fixed TypeScript fetch Error
just need to add a line of code
"lib": ["DOM"],
in yourtsconfig.json
{
// ...
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": ["es2015", "DOM"],//
// ...
}
}
refs
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
最新文章
- 让BASH,VIM美美的Powerline
- 分布式架构高可用架构篇_07_MySQL主从复制的配置(CentOS-6.7+MySQL-5.6)
- ArcGIS Server发布服务,报错001270
- 个人博客 week1
- DNS resolving 占用大量日志
- Spring boot 内存优化
- 为什么喜欢Kindle
- Codeforces Round #205 (Div. 2) : B
- Day01_UNIX基础及VI简介
- ORACLE 如何查询被锁定表及如何解锁释放session
- .net图片自动裁剪白边函数案例
- 自动清理SQLServerErrorLog错误日志避免太大
- JStorm与Storm源码分析(五)--SpoutOutputCollector与代理模式
- 将 Intent 序列化,像 Uri 一样传递 Intent!!!
- 快速开发 jQuery 插件的 10 大技巧
- python函数的面向对象——面向对象设计
- zabbix监控mysql性能
- canvas绘图history妙用
- OpenJ_Bailian 2814 拨钟问题
- Spark SQL 之 Join 实现
热门文章
- How Interfaces Work in Go
- based on Greenlets (via Eventlet and Gevent) fork 孙子worker 比较 gevent不是异步 协程原理 占位符 placeholder (Future, Promise, Deferred) 循环引擎 greenlet 没有显式调度的微线程,换言之 协程
- js控制页面元素值
- 从一片森林(JavaScript)到另一片森林(C++)
- MySql(五)SQL优化-优化SQL语句的一般步骤
- 网络编程(socket简介)
- linux系统资源限制———ulimit命令
- linux 系统磁盘管理(主分区和逻辑分区)
- (19)ln命令:在文件之间建立链接(硬链接和软链接)
- MyBatis框架使用 —— 传递多个参数的方式